| 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 | import java.io.OutputStream; |
|---|
| 24 | import java.util.HashMap; |
|---|
| 25 | import java.util.Iterator; |
|---|
| 26 | import java.util.Map; |
|---|
| 27 | |
|---|
| 28 | import javax.xml.bind.JAXBException; |
|---|
| 29 | |
|---|
| 30 | import org.docx4j.XmlUtils; |
|---|
| 31 | import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings; |
|---|
| 32 | import org.docx4j.fonts.IdentityPlusMapper; |
|---|
| 33 | import org.docx4j.fonts.Mapper; |
|---|
| 34 | import org.docx4j.fonts.PhysicalFont; |
|---|
| 35 | import org.docx4j.fonts.PhysicalFonts; |
|---|
| 36 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|---|
| 37 | import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|---|
| 38 | |
|---|
| 39 | public class CreatePdf extends AbstractSample { |
|---|
| 40 | |
|---|
| 41 | public static void main(String[] args) |
|---|
| 42 | throws Exception { |
|---|
| 43 | |
|---|
| 44 | boolean save = true; |
|---|
| 45 | |
|---|
| 46 | try { |
|---|
| 47 | getInputFilePath(args); |
|---|
| 48 | } catch (IllegalArgumentException e) { |
|---|
| 49 | inputfilepath = System.getProperty("user.dir") + "/sample-docs/word/sample-docx.xml"; |
|---|
| 50 | // inputfilepath = System.getProperty("user.dir") + "/docs/Docx4j_GettingStarted.xml"; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | WordprocessingMLPackage wordMLPackage; |
|---|
| 55 | if (inputfilepath==null) { |
|---|
| 56 | // Create a docx |
|---|
| 57 | |
|---|
| 58 | // If this is to be saved.. |
|---|
| 59 | inputfilepath = System.getProperty("user.dir") + "/tmp/output"; |
|---|
| 60 | /* |
|---|
| 61 | * NB, this currently works nicely with |
|---|
| 62 | * viaIText, and viaXSLFO (provided |
|---|
| 63 | * you view with Acrobat Reader .. it |
|---|
| 64 | * seems to overwhelm pdfviewer, which |
|---|
| 65 | * is weird, since viaIText works in both). |
|---|
| 66 | */ |
|---|
| 67 | |
|---|
| 68 | wordMLPackage = WordprocessingMLPackage.createPackage(); |
|---|
| 69 | createContent(wordMLPackage.getMainDocumentPart()); |
|---|
| 70 | } else { |
|---|
| 71 | // Load .docx or Flat OPC .xml |
|---|
| 72 | wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); |
|---|
| 73 | } |
|---|
| 74 | // Set up font mapper |
|---|
| 75 | Mapper fontMapper = new IdentityPlusMapper(); |
|---|
| 76 | wordMLPackage.setFontMapper(fontMapper); |
|---|
| 77 | |
|---|
| 78 | // Example of mapping missing font Algerian to installed font Comic Sans MS |
|---|
| 79 | PhysicalFont font |
|---|
| 80 | = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS"); |
|---|
| 81 | fontMapper.getFontMappings().put("Algerian", font); |
|---|
| 82 | |
|---|
| 83 | // As of docx4j 2.5.0, only viaXSLFO is supported. |
|---|
| 84 | // The viaIText and viaHTML source code can be found in src/docx4j-extras directory |
|---|
| 85 | |
|---|
| 86 | org.docx4j.convert.out.pdf.PdfConversion c |
|---|
| 87 | // = new org.docx4j.convert.out.pdf.viaHTML.Conversion(wordMLPackage); |
|---|
| 88 | = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage); |
|---|
| 89 | // = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage); |
|---|
| 90 | |
|---|
| 91 | ((org.docx4j.convert.out.pdf.viaXSLFO.Conversion)c).setSaveFO(new java.io.File(inputfilepath+".fo")); |
|---|
| 92 | |
|---|
| 93 | if (save) { |
|---|
| 94 | ((org.docx4j.convert.out.pdf.viaXSLFO.Conversion)c).setSaveFO( |
|---|
| 95 | new java.io.File(inputfilepath + ".fo")); |
|---|
| 96 | OutputStream os = new java.io.FileOutputStream(inputfilepath + ".pdf"); |
|---|
| 97 | c.output(os, new PdfSettings() ); |
|---|
| 98 | System.out.println("Saved " + inputfilepath + ".pdf"); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | public static void createContent(MainDocumentPart wordDocumentPart ) { |
|---|
| 103 | |
|---|
| 104 | try { |
|---|
| 105 | // Do this explicitly, since we need |
|---|
| 106 | // it in order to create our content |
|---|
| 107 | PhysicalFonts.discoverPhysicalFonts(); |
|---|
| 108 | |
|---|
| 109 | Map<String, PhysicalFont> physicalFontMap = PhysicalFonts.getPhysicalFonts(); |
|---|
| 110 | Iterator physicalFontMapIterator = physicalFontMap.entrySet().iterator(); |
|---|
| 111 | while (physicalFontMapIterator.hasNext()) { |
|---|
| 112 | Map.Entry pairs = (Map.Entry)physicalFontMapIterator.next(); |
|---|
| 113 | if(pairs.getKey()==null) { |
|---|
| 114 | pairs = (Map.Entry)physicalFontMapIterator.next(); |
|---|
| 115 | } |
|---|
| 116 | String fontName = (String)pairs.getKey(); |
|---|
| 117 | PhysicalFont pf = (PhysicalFont)pairs.getValue(); |
|---|
| 118 | |
|---|
| 119 | System.out.println("Added paragraph for " + fontName); |
|---|
| 120 | addObject(wordDocumentPart, sampleText, fontName ); |
|---|
| 121 | |
|---|
| 122 | // bold, italic etc |
|---|
| 123 | PhysicalFont pfVariation = PhysicalFonts.getBoldForm(pf); |
|---|
| 124 | if (pfVariation!=null) { |
|---|
| 125 | addObject(wordDocumentPart, sampleTextBold, pfVariation.getName() ); |
|---|
| 126 | } |
|---|
| 127 | pfVariation = PhysicalFonts.getBoldItalicForm(pf); |
|---|
| 128 | if (pfVariation!=null) { |
|---|
| 129 | addObject(wordDocumentPart, sampleTextBoldItalic, pfVariation.getName() ); |
|---|
| 130 | } |
|---|
| 131 | pfVariation = PhysicalFonts.getItalicForm(pf); |
|---|
| 132 | if (pfVariation!=null) { |
|---|
| 133 | addObject(wordDocumentPart, sampleTextItalic, pfVariation.getName() ); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | } catch (Exception e) { |
|---|
| 138 | // TODO Auto-generated catch block |
|---|
| 139 | e.printStackTrace(); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | static void addObject(MainDocumentPart wordDocumentPart, String template, String fontName ) throws JAXBException { |
|---|
| 145 | |
|---|
| 146 | HashMap substitution = new HashMap(); |
|---|
| 147 | substitution.put("fontname", fontName); |
|---|
| 148 | Object o = XmlUtils.unmarshallFromTemplate(template, substitution); |
|---|
| 149 | wordDocumentPart.addObject(o); |
|---|
| 150 | |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | final static String sampleText = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" |
|---|
| 154 | +"<w:r>" |
|---|
| 155 | +"<w:rPr>" |
|---|
| 156 | +"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />" |
|---|
| 157 | +"</w:rPr>" |
|---|
| 158 | +"<w:t xml:space=\"preserve\">${fontname}</w:t>" |
|---|
| 159 | +"</w:r>" |
|---|
| 160 | +"</w:p>"; |
|---|
| 161 | final static String sampleTextBold = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +"<w:r>" |
|---|
| 162 | +"<w:rPr>" |
|---|
| 163 | +"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />" |
|---|
| 164 | +"<w:b />" |
|---|
| 165 | +"</w:rPr>" |
|---|
| 166 | +"<w:t>${fontname} bold;</w:t>" |
|---|
| 167 | +"</w:r>" |
|---|
| 168 | +"</w:p>"; |
|---|
| 169 | final static String sampleTextItalic = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +"<w:r>" |
|---|
| 170 | +"<w:rPr>" |
|---|
| 171 | +"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />" |
|---|
| 172 | +"<w:i />" |
|---|
| 173 | +"</w:rPr>" |
|---|
| 174 | +"<w:t>${fontname} italic; </w:t>" |
|---|
| 175 | +"</w:r>" |
|---|
| 176 | +"</w:p>"; |
|---|
| 177 | final static String sampleTextBoldItalic ="<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" |
|---|
| 178 | +"<w:r>" |
|---|
| 179 | +"<w:rPr>" |
|---|
| 180 | +"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />" |
|---|
| 181 | +"<w:b />" |
|---|
| 182 | +"<w:i />" |
|---|
| 183 | +"</w:rPr>" |
|---|
| 184 | +"<w:t>${fontname} bold italic</w:t>" |
|---|
| 185 | +"</w:r>" |
|---|
| 186 | +"</w:p>"; |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | } |
|---|