| 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 | |
|---|
| 26 | import javax.xml.bind.JAXBContext; |
|---|
| 27 | import javax.xml.bind.Marshaller; |
|---|
| 28 | |
|---|
| 29 | import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator; |
|---|
| 30 | import org.docx4j.jaxb.Context; |
|---|
| 31 | import org.docx4j.jaxb.NamespacePrefixMapperUtils; |
|---|
| 32 | import org.docx4j.model.table.TblFactory; |
|---|
| 33 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 34 | import org.docx4j.openpackaging.parts.PartName; |
|---|
| 35 | import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; |
|---|
| 36 | import org.docx4j.openpackaging.contenttype.CTDefault; |
|---|
| 37 | import org.docx4j.openpackaging.contenttype.ContentType; |
|---|
| 38 | import org.docx4j.openpackaging.contenttype.ContentTypes; |
|---|
| 39 | import org.docx4j.openpackaging.contenttype.ObjectFactory; |
|---|
| 40 | import org.docx4j.openpackaging.exceptions.InvalidFormatException; |
|---|
| 41 | import org.docx4j.openpackaging.io.SaveToZipFile; |
|---|
| 42 | import org.docx4j.relationships.Relationship; |
|---|
| 43 | import org.docx4j.wml.CTAltChunk; |
|---|
| 44 | import org.docx4j.wml.Tbl; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Creates a WordprocessingML document from scratch. |
|---|
| 48 | * |
|---|
| 49 | * @author Jason Harrop |
|---|
| 50 | * @version 1.0 |
|---|
| 51 | */ |
|---|
| 52 | public class CreateWordprocessingMLDocument extends AbstractSample { |
|---|
| 53 | |
|---|
| 54 | public static void main(String[] args) throws Exception { |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | try { |
|---|
| 58 | getInputFilePath(args); |
|---|
| 59 | } catch (IllegalArgumentException e) { |
|---|
| 60 | inputfilepath = System.getProperty("user.dir") + "/CreateWordprocessingMLDocument_out.docx"; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | boolean save = |
|---|
| 64 | (inputfilepath == null ? false : true); |
|---|
| 65 | |
|---|
| 66 | System.out.println( "Creating package.."); |
|---|
| 67 | WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 68 | |
|---|
| 69 | wordMLPackage.getMainDocumentPart() |
|---|
| 70 | .addStyledParagraphOfText("Title", "Hello world"); |
|---|
| 71 | |
|---|
| 72 | wordMLPackage.getMainDocumentPart().addParagraphOfText("from docx4j!"); |
|---|
| 73 | |
|---|
| 74 | // To get bold text, you must set the run's rPr@w:b, |
|---|
| 75 | // so you can't use the createParagraphOfText convenience method |
|---|
| 76 | |
|---|
| 77 | //org.docx4j.wml.P p = wordMLPackage.getMainDocumentPart().createParagraphOfText("text"); |
|---|
| 78 | |
|---|
| 79 | org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory(); |
|---|
| 80 | org.docx4j.wml.P p = factory.createP(); |
|---|
| 81 | |
|---|
| 82 | org.docx4j.wml.Text t = factory.createText(); |
|---|
| 83 | t.setValue("text"); |
|---|
| 84 | |
|---|
| 85 | org.docx4j.wml.R run = factory.createR(); |
|---|
| 86 | run.getContent().add(t); |
|---|
| 87 | |
|---|
| 88 | p.getContent().add(run); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | org.docx4j.wml.RPr rpr = factory.createRPr(); |
|---|
| 92 | org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue(); |
|---|
| 93 | b.setVal(true); |
|---|
| 94 | rpr.setB(b); |
|---|
| 95 | |
|---|
| 96 | run.setRPr(rpr); |
|---|
| 97 | |
|---|
| 98 | // Optionally, set pPr/rPr@w:b |
|---|
| 99 | org.docx4j.wml.PPr ppr = factory.createPPr(); |
|---|
| 100 | p.setPPr( ppr ); |
|---|
| 101 | org.docx4j.wml.ParaRPr paraRpr = factory.createParaRPr(); |
|---|
| 102 | ppr.setRPr(paraRpr); |
|---|
| 103 | rpr.setB(b); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | wordMLPackage.getMainDocumentPart().addObject(p); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | // Here is an easier way: |
|---|
| 110 | String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:t>Bold, just at w:r level</w:t></w:r></w:p>"; |
|---|
| 111 | |
|---|
| 112 | wordMLPackage.getMainDocumentPart().addObject( |
|---|
| 113 | org.docx4j.XmlUtils.unmarshalString(str) ); |
|---|
| 114 | |
|---|
| 115 | // Let's add a table |
|---|
| 116 | int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips(); |
|---|
| 117 | int cols = 3; |
|---|
| 118 | int cellWidthTwips = new Double( |
|---|
| 119 | Math.floor( (writableWidthTwips/cols )) |
|---|
| 120 | ).intValue(); |
|---|
| 121 | |
|---|
| 122 | Tbl tbl = TblFactory.createTable(3, 3, cellWidthTwips); |
|---|
| 123 | wordMLPackage.getMainDocumentPart().addObject(tbl); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | // Add an altChunk |
|---|
| 127 | // .. the part |
|---|
| 128 | String html = "<html><head><title>Import me</title></head><body><p>Hello World!</p></body></html>"; |
|---|
| 129 | AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html") ); |
|---|
| 130 | afiPart.setBinaryData(html.getBytes()); |
|---|
| 131 | afiPart.setContentType(new ContentType("text/html")); |
|---|
| 132 | Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart); |
|---|
| 133 | // .. the bit in document body |
|---|
| 134 | CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk(); |
|---|
| 135 | ac.setId(altChunkRel.getId() ); |
|---|
| 136 | wordMLPackage.getMainDocumentPart().addObject(ac); |
|---|
| 137 | |
|---|
| 138 | // .. content type |
|---|
| 139 | wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html"); |
|---|
| 140 | |
|---|
| 141 | //injectDocPropsCustomPart(wordMLPackage); |
|---|
| 142 | |
|---|
| 143 | // Now save it |
|---|
| 144 | if (save) { |
|---|
| 145 | wordMLPackage.save(new java.io.File(inputfilepath) ); |
|---|
| 146 | System.out.println("Saved " + inputfilepath); |
|---|
| 147 | } else { |
|---|
| 148 | // Create a org.docx4j.wml.Package object |
|---|
| 149 | FlatOpcXmlCreator worker = new FlatOpcXmlCreator(wordMLPackage); |
|---|
| 150 | org.docx4j.xmlPackage.Package pkg = worker.get(); |
|---|
| 151 | |
|---|
| 152 | // Now marshall it |
|---|
| 153 | JAXBContext jc = Context.jcXmlPackage; |
|---|
| 154 | Marshaller marshaller=jc.createMarshaller(); |
|---|
| 155 | |
|---|
| 156 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); |
|---|
| 157 | NamespacePrefixMapperUtils.setProperty(marshaller, |
|---|
| 158 | NamespacePrefixMapperUtils.getPrefixMapper()); |
|---|
| 159 | System.out.println( "\n\n OUTPUT " ); |
|---|
| 160 | System.out.println( "====== \n\n " ); |
|---|
| 161 | marshaller.marshal(pkg, System.out); |
|---|
| 162 | |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | System.out.println("Done."); |
|---|
| 166 | |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | public static void injectDocPropsCustomPart(WordprocessingMLPackage wordMLPackage) { |
|---|
| 170 | |
|---|
| 171 | try { |
|---|
| 172 | org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart = new org.docx4j.openpackaging.parts.DocPropsCustomPart(); |
|---|
| 173 | |
|---|
| 174 | java.io.InputStream is = new java.io.FileInputStream("/tmp/custompart.xml" ); |
|---|
| 175 | |
|---|
| 176 | docPropsCustomPart.unmarshal(is); |
|---|
| 177 | |
|---|
| 178 | wordMLPackage.addTargetPart(docPropsCustomPart); |
|---|
| 179 | |
|---|
| 180 | } catch (Exception e) { |
|---|
| 181 | // TODO Auto-generated catch block |
|---|
| 182 | e.printStackTrace(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | } |
|---|