jWordConvert Developer Guide
jWordConvert Developer Guide
jWordConvert is a Java library that can read Word documents and then convert to PDF, or render them into images or to a printer. jWordConvert can be integrated into your Java application or applet to allow you to provide conversion functionality to your users. jWordConvert provides the following functions:
- Convert Word document (doc and docx) to PDF
- Set permissions and passwords on the converted PDF documents
- Convert pages to JPEG, TIFF or PNG images
- Print Word documents automatically
- No third party software or drivers necessary
- Support for JDK 1.6 and above
- Save to the file system or to Java output streams
Like all of our libraries, jWordConvert is built using Qoppa’s proprietary technology and so does not require any third party software or drivers.
The starting point for using jWordConvert is the com.qoppa.word.WordDocument class. This class is used to load Word documents and then provides methods to save as a PDF document, to convert to images and to print the document.
The class provides constructors to load Word documents from files, URLs, input streams or byte arrays. To convert a Word file from the file system to PDF, there are only a few lines of code needed to set the conversion options, load the Word document and then save it to PDF:
// Create an Object to store the conversion options WordConvertOptions opts = new WordConvertOptions(); // Set option to embed fonts to true opts.setEmbedded(true); // Suggested Fallback Font for Windows with Microsoft Office opts.setFallbackFontPath("C:/Windows/Fonts/ARIALUNI.ttf"); // Open / Convert the Word Document using the conversion options WordDocument wdoc = new WordDocument("C:/input.doc", options); // Convert and save as PDF wdoc.saveAsPDF("C:/out.pdf");
Read more about the options to embed or not embed fonts.
Once a Word document has been loaded by instantiating the WordDocument class, pages in the document can converted to image files. jWordConvert can convert pages to JPEG, PNG and TIFF files.
// Save the first page to a PNG file wdoc.savePageAsPNG (0, "page0.png", 150); // Save the entire document as a TIFF file TIFFOptions options = new TIFFOptions (150, TIFFOptions.TIFF_FAX_GROUP4); wdoc.saveDocumentAsTIFF ("doc.tif", options);
jWordConvert can also print Word documents, the WordDocument class provides a number of print methods to allow printing to the default printer, to a named printer, or to prompt the user for the printer.
To print to the default printer, or to print to a named printer, use the following lines of code:
// Print to the default printer wdoc.printToDefaultPrinter(); // Print to a named printer wdoc.print ("my printer");
jWordConvert is packaged in a single jar file, jwordconvert.jar that needs to be included in the class path when running an application that uses jWordConvert.