Converting Word to Pdf in Java Example
I am updating this post to provide,
- maven project with all the required dependencies
- Resolved log4j warning at the top of the converted pdf.
Table of Contents
Converting Word to Pdf in Java Example:
pom.xml:
To keep the post clean I kept only the required portion, please download the zip file for complete files.
<dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-ImportXHTML</artifactId> <version>3.0.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>
WordToPDFConvert.java:
Please create the folder named mirthbees under your c drive(if you want to create in other places or other names, accordingly update the path details in the below program inputWordPath & outputPDFPath varaibles) and have the input word document there, ensure it is not opened when you running this conversion program.
package com.ngdeveloper; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import org.docx4j.Docx4J; import org.docx4j.fonts.IdentityPlusMapper; import org.docx4j.fonts.Mapper; import org.docx4j.model.structure.SectionWrapper; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; /** * This converts Docx file to PDF * Author: Naveen - Javadomain.in */ public class WordToPDFConvert { public static void main(String[] args) { String inputWordPath = "C:\\mirthbees\\javadomain.docx"; String outputPDFPath = "C:\\mirthbees\\javadomain.pdf"; try { System.err.println("Word Document to PDF Convert Begins!"); InputStream is = new FileInputStream(new File(inputWordPath)); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is); List sections = wordMLPackage.getDocumentModel().getSections(); for (int i = 0; i < sections.size(); i++) { wordMLPackage.getDocumentModel().getSections().get(i).getPageDimensions().setHeaderExtent(3000); } Mapper fontMapper = new IdentityPlusMapper(); // For font specific, enable the below lines // PhysicalFont font = PhysicalFonts.getPhysicalFonts().get("Comic // Sans MS"); // fontMapper.getFontMappings().put("Algerian", font); wordMLPackage.setFontMapper(fontMapper); Docx4J.toPDF(wordMLPackage, new FileOutputStream(outputPDFPath)); System.err.println("Your Word Document Converted to PDF Successfully!"); } catch (Exception e) { e.printStackTrace(); } } }
Resolving “To HIDE THESE MESSAGES, TURN OFF log4j debug level logging for org.docx4j.convert.out.pdf.viaXSLFO”
Create docx4j.properties in your classpath (src/main/resources) and paste the below file contents to resolve this “To HIDE THESE MESSAGES, TURN OFF log4j debug level logging for org.docx4j.convert.out.pdf.viaXSLFO”
New Maven Project Input & Output Screenshots :
Input Word File:
Output PDF File:
[Below for your reference only, for better results, use the above maven projects only.]
Old Post: Just for reference only, check the above mentioned maven project only.
Converting Word to Pdf in Java:
Jars:
avalon-framework-4.1.5
commons-io-2.4
docx4j-2.7.1
log4j-1.2.15
serializer-2.7.1
xmlgraphics-commons-1.3
batik-util-1.6-1
commons-logging-1.1.3
fop-0.93
xalan-2.7.1
Java Program:
package in.javadomain; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings; import org.docx4j.fonts.IdentityPlusMapper; import org.docx4j.fonts.Mapper; import org.docx4j.fonts.PhysicalFont; import org.docx4j.fonts.PhysicalFonts; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; public class Word2Pdf { public static void main(String[] args) { try { long start = System.currentTimeMillis(); InputStream is = new FileInputStream( new File("D:\\javadomain.docx")); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage .load(is); List sections = wordMLPackage.getDocumentModel().getSections(); for (int i = 0; i < sections.size(); i++) { System.out.println("sections Size" + sections.size()); wordMLPackage.getDocumentModel().getSections().get(i) .getPageDimensions().setHeaderExtent(3000); } Mapper fontMapper = new IdentityPlusMapper(); PhysicalFont font = PhysicalFonts.getPhysicalFonts().get( "Comic Sans MS"); fontMapper.getFontMappings().put("Algerian", font); wordMLPackage.setFontMapper(fontMapper); PdfSettings pdfSettings = new PdfSettings(); org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion( wordMLPackage); OutputStream out = new FileOutputStream(new File( "D:\\javadomain.pdf")); conversion.output(out, pdfSettings); System.err.println("Time taken to Generate pdf " + (System.currentTimeMillis() - start) + "ms"); } catch (Exception e) { e.printStackTrace(); } } }
Input [word file]
Output [pdf file]
Recommended Books:
If the code above does not work for all your Word documents, please consider our java library jWordConvert.
It takes 2 lines to convert a Word document to PDF:
// Load the Word document
WordDocument wdoc = new WordDocument ("input.doc");
// Save the document as a PDF file
wdoc.saveAsPDF ("output.pdf");
Hi Mirthbees ,
Your tutorial is good. I have generated PDF file. But ‘ TO HIDE THESE MESSAGES, TURN OFF log4j debug level logging for org.docx4j.convert.out.pdf.viaXSLFO ‘ message come by default. So to hide this message , I have used ” org.docx4j.convert.out.pdf.viaXSLFO.Conversion.log.setLevel(Level.OFF); ” line. But problem is when generating war its making problem.