| 1 | /* |
|---|
| 2 | * Copyright 2007-2008, Plutext Pty Ltd. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of docx4j. |
|---|
| 5 | |
|---|
| 6 | docx4j is licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 7 | you may not use this file except in compliance with the License. |
|---|
| 8 | |
|---|
| 9 | You may obtain a copy of the License at |
|---|
| 10 | |
|---|
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 12 | |
|---|
| 13 | Unless required by applicable law or agreed to in writing, software |
|---|
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 16 | See the License for the specific language governing permissions and |
|---|
| 17 | limitations under the License. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | package org.docx4j.samples; |
|---|
| 23 | |
|---|
| 24 | import javax.xml.bind.JAXBElement; |
|---|
| 25 | |
|---|
| 26 | import org.docx4j.jaxb.Context; |
|---|
| 27 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 28 | import org.docx4j.openpackaging.parts.relationships.Namespaces; |
|---|
| 29 | |
|---|
| 30 | import org.docx4j.XmlUtils; |
|---|
| 31 | import org.docx4j.wml.CTRel; |
|---|
| 32 | import org.docx4j.wml.P; |
|---|
| 33 | import org.docx4j.wml.P.Hyperlink; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Master and Subdocuments. |
|---|
| 38 | * |
|---|
| 39 | * A subdocument is just an external part. |
|---|
| 40 | * |
|---|
| 41 | * So this sample is very similar to the hyperlink one. |
|---|
| 42 | * |
|---|
| 43 | * @author Jason Harrop |
|---|
| 44 | * @version 1.0 |
|---|
| 45 | */ |
|---|
| 46 | public class SubDocument { |
|---|
| 47 | |
|---|
| 48 | /* |
|---|
| 49 | * |
|---|
| 50 | <w:p> |
|---|
| 51 | <w:pPr> |
|---|
| 52 | <w:pStyle w:val="Heading1"/> |
|---|
| 53 | </w:pPr> |
|---|
| 54 | <w:subDoc r:id="rId4"/> |
|---|
| 55 | <w:r> |
|---|
| 56 | <w:t>Another heading</w:t> |
|---|
| 57 | </w:r> |
|---|
| 58 | </w:p> |
|---|
| 59 | |
|---|
| 60 | * word/_rels/document.xml.rels contains: |
|---|
| 61 | * |
|---|
| 62 | * <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/subDocument" |
|---|
| 63 | * Target="http://dev.plutext.org/" TargetMode="External"/> |
|---|
| 64 | |
|---|
| 65 | */ |
|---|
| 66 | |
|---|
| 67 | public static void main(String[] args) throws Exception { |
|---|
| 68 | |
|---|
| 69 | // Create the master doc, and specify |
|---|
| 70 | // the subdoc |
|---|
| 71 | String subdocx = "sample-docx.xml"; |
|---|
| 72 | |
|---|
| 73 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 74 | |
|---|
| 75 | // Link to subdoc |
|---|
| 76 | JAXBElement<CTRel> subdoc = createSubdoc(wordMLPackage, subdocx); |
|---|
| 77 | |
|---|
| 78 | // Add it to a paragraph |
|---|
| 79 | org.docx4j.wml.ObjectFactory wmlFactory = new org.docx4j.wml.ObjectFactory(); |
|---|
| 80 | org.docx4j.wml.P paragraph = wmlFactory.createP(); |
|---|
| 81 | |
|---|
| 82 | paragraph.getParagraphContent().add( subdoc ); |
|---|
| 83 | wordMLPackage.getMainDocumentPart().addObject(paragraph); |
|---|
| 84 | |
|---|
| 85 | // Now save it |
|---|
| 86 | wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/out-master.docx") ); |
|---|
| 87 | |
|---|
| 88 | System.out.println("Done."); |
|---|
| 89 | |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public static JAXBElement<CTRel> createSubdoc(WordprocessingMLPackage wordMLPackage, |
|---|
| 93 | String subdocName) { |
|---|
| 94 | |
|---|
| 95 | try { |
|---|
| 96 | |
|---|
| 97 | // We need to add a relationship to word/_rels/document.xml.rels |
|---|
| 98 | // but since its external, we don't use the |
|---|
| 99 | // usual wordMLPackage.getMainDocumentPart().addTargetPart |
|---|
| 100 | // mechanism |
|---|
| 101 | org.docx4j.relationships.ObjectFactory factory = |
|---|
| 102 | new org.docx4j.relationships.ObjectFactory(); |
|---|
| 103 | |
|---|
| 104 | org.docx4j.relationships.Relationship rel = factory.createRelationship(); |
|---|
| 105 | rel.setType( Namespaces.SUBDOCUMENT ); |
|---|
| 106 | rel.setTarget(subdocName); |
|---|
| 107 | rel.setTargetMode("External"); |
|---|
| 108 | |
|---|
| 109 | wordMLPackage.getMainDocumentPart().getRelationshipsPart().addRelationship(rel); |
|---|
| 110 | |
|---|
| 111 | // addRelationship sets the rel's @Id |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | org.docx4j.wml.ObjectFactory wmlOF = new org.docx4j.wml.ObjectFactory(); |
|---|
| 115 | |
|---|
| 116 | CTRel ctRel = wmlOF.createCTRel(); |
|---|
| 117 | ctRel.setId(rel.getId()); |
|---|
| 118 | |
|---|
| 119 | return wmlOF.createPSubDoc(ctRel); |
|---|
| 120 | |
|---|
| 121 | } catch (Exception e) { |
|---|
| 122 | // TODO Auto-generated catch block |
|---|
| 123 | e.printStackTrace(); |
|---|
| 124 | return null; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | } |
|---|
| 131 | |
|---|