| 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.JAXBContext; |
|---|
| 25 | import javax.xml.bind.Marshaller; |
|---|
| 26 | |
|---|
| 27 | import org.docx4j.XmlUtils; |
|---|
| 28 | import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator; |
|---|
| 29 | import org.docx4j.jaxb.Context; |
|---|
| 30 | import org.docx4j.jaxb.NamespacePrefixMapperUtils; |
|---|
| 31 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 32 | import org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart; |
|---|
| 33 | import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; |
|---|
| 34 | import org.docx4j.wml.CTRel; |
|---|
| 35 | import org.docx4j.wml.CTSettings; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Creates a WordprocessingML document from scratch, |
|---|
| 39 | * and attaches a template |
|---|
| 40 | * |
|---|
| 41 | * In Flat OPC terms, aim is to produce: |
|---|
| 42 | * |
|---|
| 43 | <pkg:part pkg:name="/word/settings.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"> |
|---|
| 44 | <pkg:xmlData> |
|---|
| 45 | <w:settings xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main"> |
|---|
| 46 | <w:attachedTemplate r:id="rId1"/> |
|---|
| 47 | </w:settings> |
|---|
| 48 | </pkg:xmlData> |
|---|
| 49 | </pkg:part> |
|---|
| 50 | * <pkg:part pkg:name="/word/_rels/settings.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml"> |
|---|
| 51 | <pkg:xmlData> |
|---|
| 52 | <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
|---|
| 53 | <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" Target="file:///C:\Users\jharrop\AppData\Roaming\Microsoft\Templates\fabdocx-release-20101002B.dotm" TargetMode="External"/> |
|---|
| 54 | </Relationships> |
|---|
| 55 | </pkg:xmlData> |
|---|
| 56 | </pkg:part> |
|---|
| 57 | |
|---|
| 58 | * |
|---|
| 59 | * @author Jason Harrop |
|---|
| 60 | * @version 1.0 |
|---|
| 61 | */ |
|---|
| 62 | public class TemplateAttach extends AbstractSample { |
|---|
| 63 | |
|---|
| 64 | public static void main(String[] args) throws Exception { |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | try { |
|---|
| 68 | getInputFilePath(args); |
|---|
| 69 | } catch (IllegalArgumentException e) { |
|---|
| 70 | inputfilepath = System.getProperty("user.dir") + "/TemplateAttach_out.docx"; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | boolean save = |
|---|
| 74 | (inputfilepath == null ? false : true); |
|---|
| 75 | |
|---|
| 76 | System.out.println( "Creating package.."); |
|---|
| 77 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 78 | |
|---|
| 79 | wordMLPackage.getMainDocumentPart() |
|---|
| 80 | .addStyledParagraphOfText("Title", "Hello world"); |
|---|
| 81 | |
|---|
| 82 | wordMLPackage.getMainDocumentPart().addParagraphOfText("from docx4j!"); |
|---|
| 83 | |
|---|
| 84 | // Create settings part, and init content |
|---|
| 85 | DocumentSettingsPart dsp = new DocumentSettingsPart(); |
|---|
| 86 | CTSettings settings = Context.getWmlObjectFactory().createCTSettings(); |
|---|
| 87 | dsp.setJaxbElement(settings); |
|---|
| 88 | wordMLPackage.getMainDocumentPart().addTargetPart(dsp); |
|---|
| 89 | |
|---|
| 90 | // Create external rel |
|---|
| 91 | RelationshipsPart rp = RelationshipsPart.createRelationshipsPartForPart(dsp); |
|---|
| 92 | org.docx4j.relationships.Relationship rel = new org.docx4j.relationships.ObjectFactory().createRelationship(); |
|---|
| 93 | rel.setType( "http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate" ); |
|---|
| 94 | rel.setTarget("file:///C:\\Users\\jsmith\\AppData\\Roaming\\Microsoft\\Templates\\yours.dotm"); |
|---|
| 95 | rel.setTargetMode("External"); |
|---|
| 96 | rp.addRelationship(rel); // addRelationship sets the rel's @Id |
|---|
| 97 | |
|---|
| 98 | settings.setAttachedTemplate( |
|---|
| 99 | (CTRel)XmlUtils.unmarshalString("<w:attachedTemplate xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:id=\"" + rel.getId() + "\"/>", Context.jc, CTRel.class) |
|---|
| 100 | ); |
|---|
| 101 | |
|---|
| 102 | // or (yuck)... |
|---|
| 103 | // CTRel id = new CTRel(); |
|---|
| 104 | // id.setId( rel.getId() ); |
|---|
| 105 | // JAXBElement<CTRel> je = new JAXBElement<CTRel>( |
|---|
| 106 | // new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "attachedTemplate"), |
|---|
| 107 | // CTRel.class, null, id); |
|---|
| 108 | // settings.setAttachedTemplate(je.getValue()); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | // Now save it |
|---|
| 113 | if (save) { |
|---|
| 114 | wordMLPackage.save(new java.io.File(inputfilepath) ); |
|---|
| 115 | System.out.println("Saved " + inputfilepath); |
|---|
| 116 | } else { |
|---|
| 117 | // Create a org.docx4j.wml.Package object |
|---|
| 118 | FlatOpcXmlCreator worker = new FlatOpcXmlCreator(wordMLPackage); |
|---|
| 119 | org.docx4j.xmlPackage.Package pkg = worker.get(); |
|---|
| 120 | |
|---|
| 121 | // Now marshall it |
|---|
| 122 | JAXBContext jc = Context.jcXmlPackage; |
|---|
| 123 | Marshaller marshaller=jc.createMarshaller(); |
|---|
| 124 | |
|---|
| 125 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); |
|---|
| 126 | NamespacePrefixMapperUtils.setProperty(marshaller, |
|---|
| 127 | NamespacePrefixMapperUtils.getPrefixMapper()); |
|---|
| 128 | System.out.println( "\n\n OUTPUT " ); |
|---|
| 129 | System.out.println( "====== \n\n " ); |
|---|
| 130 | marshaller.marshal(pkg, System.out); |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | System.out.println("Done."); |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | } |
|---|