| 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 | package org.docx4j.samples; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | import java.util.List; |
|---|
| 25 | |
|---|
| 26 | import javax.xml.bind.JAXBElement; |
|---|
| 27 | |
|---|
| 28 | import org.docx4j.openpackaging.io.LoadFromZipFile; |
|---|
| 29 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 30 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 31 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 32 | import org.docx4j.wml.Body; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | public class DocProps extends AbstractSample { |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * @param args |
|---|
| 39 | */ |
|---|
| 40 | public static void main(String[] args) throws Exception { |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | try { |
|---|
| 45 | getInputFilePath(args); |
|---|
| 46 | } catch (IllegalArgumentException e) { |
|---|
| 47 | inputfilepath = System.getProperty("user.dir") + "/sample-docs/docProps.docx"; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | boolean save = false; |
|---|
| 52 | try { |
|---|
| 53 | getOutputFilePath(args); |
|---|
| 54 | save = true; |
|---|
| 55 | } catch (IllegalArgumentException e) { |
|---|
| 56 | outputfilepath = System.getProperty("user.dir") + "/sample-docs/docProps-out.docx"; |
|---|
| 57 | |
|---|
| 58 | // save = true; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // Open a document from the file system |
|---|
| 62 | // 1. Load the Package |
|---|
| 63 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 64 | |
|---|
| 65 | // Let's look at the core properties |
|---|
| 66 | org.docx4j.openpackaging.parts.DocPropsCorePart docPropsCorePart = wordMLPackage.getDocPropsCorePart(); |
|---|
| 67 | org.docx4j.docProps.core.CoreProperties coreProps = (org.docx4j.docProps.core.CoreProperties)docPropsCorePart.getJaxbElement(); |
|---|
| 68 | |
|---|
| 69 | // What is the title of the document? |
|---|
| 70 | System.out.println("'dc:title' is " + coreProps.getTitle().getValue().getContent().get(0)); |
|---|
| 71 | // Yuck! TODO: Simplify the dc schema. |
|---|
| 72 | |
|---|
| 73 | System.out.println(coreProps.getTitle().getValue().getClass().getName() ); |
|---|
| 74 | // returns org.docx4j.docProps.core.dc.elements.SimpleLiteral as expected |
|---|
| 75 | |
|---|
| 76 | System.out.println("'dcterms:created' is " + coreProps.getCreated().getClass().getName() ); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | //System.out.println(coreProps.getTitle().getValue() instanceof ) |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | // Let's look at the extended properties |
|---|
| 83 | org.docx4j.openpackaging.parts.DocPropsExtendedPart docPropsExtendedPart = wordMLPackage.getDocPropsExtendedPart(); |
|---|
| 84 | org.docx4j.docProps.extended.Properties extendedProps = (org.docx4j.docProps.extended.Properties)docPropsExtendedPart.getJaxbElement(); |
|---|
| 85 | |
|---|
| 86 | // What application was the document created with? |
|---|
| 87 | System.out.println("'Application' is " + extendedProps.getApplication() + " v." + extendedProps.getAppVersion()); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | // Finally, the custom properties |
|---|
| 91 | org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = wordMLPackage.getDocPropsCustomPart(); |
|---|
| 92 | org.docx4j.docProps.custom.Properties customProps = (org.docx4j.docProps.custom.Properties)docPropsCustomPart.getJaxbElement(); |
|---|
| 93 | |
|---|
| 94 | for (org.docx4j.docProps.custom.Properties.Property prop: customProps.getProperty() ) { |
|---|
| 95 | |
|---|
| 96 | System.out.println(prop.getName()); |
|---|
| 97 | |
|---|
| 98 | // At the moment, you need to know what sort of value it has. |
|---|
| 99 | // Could create a generic Object getValue() method. |
|---|
| 100 | |
|---|
| 101 | System.out.println(prop.getLpwstr()); |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // Ok, let's add one. |
|---|
| 106 | org.docx4j.docProps.custom.ObjectFactory factory = new org.docx4j.docProps.custom.ObjectFactory(); |
|---|
| 107 | org.docx4j.docProps.custom.Properties.Property newProp = factory.createPropertiesProperty(); |
|---|
| 108 | |
|---|
| 109 | // .. set it up |
|---|
| 110 | newProp.setName("mynewcustomprop"); |
|---|
| 111 | newProp.setFmtid(docPropsCustomPart.fmtidValLpwstr ); // Magic string |
|---|
| 112 | newProp.setPid( customProps.getNextId() ); |
|---|
| 113 | newProp.setLpwstr("SomeValue"); |
|---|
| 114 | |
|---|
| 115 | // .. add it |
|---|
| 116 | customProps.getProperty().add(newProp); |
|---|
| 117 | |
|---|
| 118 | /* Example of setting some extended properties |
|---|
| 119 | DocPropsExtendedPart extendedPart = new DocPropsExtendedPart(); |
|---|
| 120 | wordMLPackage.addTargetPart(extendedPart); |
|---|
| 121 | // Add content type |
|---|
| 122 | ContentTypeManager ctm = wordMLPackage.getContentTypeManager(); |
|---|
| 123 | ctm.addOverrideContentType(extendedPart.getPartName().getURI(), extendedPart.getContentType()); |
|---|
| 124 | // Now the properties |
|---|
| 125 | org.docx4j.docProps.extended.ObjectFactory factory = new org.docx4j.docProps.extended.ObjectFactory(); |
|---|
| 126 | Properties props = factory.createProperties(); |
|---|
| 127 | props.setApplication("Microsoft Word 12.1.0"); |
|---|
| 128 | props.setAppVersion("12.0256"); |
|---|
| 129 | extendedPart.setJaxbElement(props);` |
|---|
| 130 | //Required for docx4j < 2.2.0 |
|---|
| 131 | extendedPart.setJAXBContext(Context.jcDocPropsExtended); |
|---|
| 132 | */ |
|---|
| 133 | |
|---|
| 134 | // Save the revised document |
|---|
| 135 | |
|---|
| 136 | if (save) { |
|---|
| 137 | SaveToZipFile saver = new SaveToZipFile(wordMLPackage); |
|---|
| 138 | saver.save(outputfilepath); |
|---|
| 139 | System.out.println("Document saved as " + outputfilepath); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | System.out.println("Done."); |
|---|
| 143 | |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | } |
|---|