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