| 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 java.io.File; |
|---|
| 25 | import java.util.UUID; |
|---|
| 26 | |
|---|
| 27 | import org.docx4j.customXmlProperties.DatastoreItem; |
|---|
| 28 | import org.docx4j.jaxb.Context; |
|---|
| 29 | import org.docx4j.model.datastorage.CustomXmlDataStorage; |
|---|
| 30 | import org.docx4j.model.datastorage.CustomXmlDataStorageImpl; |
|---|
| 31 | import org.docx4j.openpackaging.Base; |
|---|
| 32 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 33 | import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart; |
|---|
| 34 | import org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart; |
|---|
| 35 | import org.docx4j.openpackaging.parts.Part; |
|---|
| 36 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 37 | import org.docx4j.openpackaging.parts.Parts; |
|---|
| 38 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 39 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 40 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 41 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Creates a docx containing a CustomXml part. |
|---|
| 45 | * |
|---|
| 46 | * @author Jason Harrop |
|---|
| 47 | * @version 1.0 |
|---|
| 48 | */ |
|---|
| 49 | public class CreateDocxWithCustomXml { |
|---|
| 50 | |
|---|
| 51 | public static void main(String[] args) throws Exception { |
|---|
| 52 | |
|---|
| 53 | System.out.println( "Creating package.."); |
|---|
| 54 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 55 | |
|---|
| 56 | wordMLPackage.getMainDocumentPart() |
|---|
| 57 | .addStyledParagraphOfText("Title", "Hello world"); |
|---|
| 58 | |
|---|
| 59 | wordMLPackage.getMainDocumentPart().addParagraphOfText("from docx4j!"); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | CustomXmlDataStoragePart customXmlDataStoragePart = injectCustomXmlDataStoragePart(wordMLPackage.getMainDocumentPart(), |
|---|
| 63 | wordMLPackage.getParts() ); |
|---|
| 64 | |
|---|
| 65 | addProperties(customXmlDataStoragePart); |
|---|
| 66 | |
|---|
| 67 | // Now save it |
|---|
| 68 | wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/customxml2.docx") ); |
|---|
| 69 | |
|---|
| 70 | System.out.println("Done."); |
|---|
| 71 | |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | public static CustomXmlDataStoragePart injectCustomXmlDataStoragePart(Part parent, Parts parts) throws Exception { |
|---|
| 76 | |
|---|
| 77 | org.docx4j.openpackaging.parts.CustomXmlDataStoragePart customXmlDataStoragePart = |
|---|
| 78 | new org.docx4j.openpackaging.parts.CustomXmlDataStoragePart(parts); |
|---|
| 79 | // Defaults to /customXml/item1.xml |
|---|
| 80 | |
|---|
| 81 | CustomXmlDataStorage data = new CustomXmlDataStorageImpl(); |
|---|
| 82 | data.setDocument(createCustomXmlDocument()); |
|---|
| 83 | |
|---|
| 84 | customXmlDataStoragePart.setData(data); |
|---|
| 85 | |
|---|
| 86 | // customXmlDataStoragePart.setDocument( createCustomXmlDocument() ); |
|---|
| 87 | |
|---|
| 88 | parent.addTargetPart(customXmlDataStoragePart); |
|---|
| 89 | |
|---|
| 90 | return customXmlDataStoragePart; |
|---|
| 91 | |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | public static void addProperties(CustomXmlDataStoragePart customXmlDataStoragePart) throws InvalidFormatException { |
|---|
| 95 | |
|---|
| 96 | CustomXmlDataStoragePropertiesPart part = new CustomXmlDataStoragePropertiesPart(); |
|---|
| 97 | |
|---|
| 98 | org.docx4j.customXmlProperties.ObjectFactory of = new org.docx4j.customXmlProperties.ObjectFactory(); |
|---|
| 99 | |
|---|
| 100 | DatastoreItem dsi = of.createDatastoreItem(); |
|---|
| 101 | String newItemId = "{" + UUID.randomUUID().toString() + "}"; |
|---|
| 102 | dsi.setItemID(newItemId); |
|---|
| 103 | |
|---|
| 104 | part.setJaxbElement(dsi ); |
|---|
| 105 | |
|---|
| 106 | customXmlDataStoragePart.addTargetPart(part); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | public static org.w3c.dom.Document createCustomXmlDocument() { |
|---|
| 110 | |
|---|
| 111 | // TODO: implement |
|---|
| 112 | |
|---|
| 113 | return null; |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | } |
|---|