jOfficeConvert Developer Guide
jOfficeConvert Developer Guide
Contents
Introduction
Convert Word To PDF
Convert Word To Images
Print Word Documents
Convert Excel To PDF
Convert Excel To Images
Print Excel Documents
Convert PowerPoint To PDF
Convert PowerPoint To Images
Distribution and JAR files
Javadoc API
Source Code Samples
Introduction
jOfficeConvert is a Java library that can read Word documents and then convert to PDF, or render them into images or to a printer. jOfficeConvert can be integrated into your Java application to allow you to provide conversion functionality to your users. jOfficeConvert provides the following functions:
- Convert Word documents (doc and docx) to PDF
- Convert Excel spreadsheets (xlsx) 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.8 and above
- Save to the file system or to Java output streams
Like all of our libraries, jOfficeConvert is built using Qoppa’s proprietary technology and so does not require any third party software or drivers.
Convert Word To PDF
The starting point is the com.qoppa.office.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.
Converting Word to Images
Once a Word document has been loaded by instantiating the WordDocument class, pages in the document can converted to image files. jOfficeConvert can convert pages to JPEG, PNG and TIFF files.
// Open / Convert the Word Document with default options WordDocument wdoc = new WordDocument("C:/input.doc", new WordConvertOptions()); // 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); |
Print Word Documents
jOfficeConvert 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:
// Open / Convert the Word Document with default options WordDocument wdoc = new WordDocument("C:/input.doc", new WordConvertOptions()); // Print to the default printer wdoc.printToDefaultPrinter(); // Print to a named printer wdoc.print ("my printer"); |
Convert Excel To PDF
The starting point is the com.qoppa.office.ExcelDocument class. This class is used to load Excel spreadsheet 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 Excel documents from files, URLs, input streams or byte arrays. To convert an Excel file from the file system to PDF, there are only a few lines of code needed to set the conversion options, load the Excel document and then save it to PDF:
// Load the document ExcelDocument ed = new ExcelDocument("input.xlsx", new ExcelConvertOptions()); // Save the document as a PDF file ed.saveAsPDF("output.pdf"); |
Converting Excel to Images
Once an Excel spreadsheet has been loaded by instantiating the ExcelDocument class, worksheets in the document can converted to image files. jOfficeConvert can convert pages to JPEG, PNG and TIFF files.
// Load the document ExcelDocument ed = new ExcelDocument("input.xlsx", new ExcelConvertOptions()); // Save the document as a multi-page TIFF file ed.saveDocumentAsTIFF("output.tif", new TIFFOptions(150, TIFFOptions.TIFF_FAX_GROUP4)); |
Print Excel Documents
jOfficeConvert can also print Excel documents, the ExcelDocument 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:
// Load the document ExcelDocument ed = new ExcelDocument("input.xlsx", new ExcelConvertOptions()); // Print to the default printer ed.printToDefaultPrinter(null); // Print to a named printer ed.print("my printer", null); |
Convert PowerPoint To PDF
The starting point is the com.qoppa.office.PowerPointDocument class. This class is used to load a PowerPoint presentation and then provides methods to save as a PDF document, and to convert to images.
The class provides constructors to load PowerPoint documents from files, URLs, input streams or byte arrays. To convert a PowerPoint file from the file system to PDF, there are only a few lines of code needed to set the conversion options, load the PowerPoint document and then save it to PDF:
// Load the document PowerPointDocument pd = new PowerPointDocument("input.pptx", new PowerPointConvertOptions()); // Save the document as a PDF file pd.saveAsPDF("output.pdf"); |
Converting PowerPoint to Images
Once a PowerPoint presentation has been loaded by instantiating the PowerPointDocument class, slides in the document can converted to image files. jOfficeConvert can convert pages to JPEG, PNG and TIFF files.
// Load the document PowerPointDocument pd = new PowerPointDocument("input.pptx", new PowerPointConvertOptions()); // Save the document as a multi-page TIFF file pd.saveDocumentAsTIFF("output.tif", new TIFFOptions(150, TIFFOptions.TIFF_FAX_GROUP4)); |
Distribution and JAR Files
Required and optional jar files for jOfficeConvert can be found on the jOfficeConvert Download page.