NOW AVAILABLE: Summer 2025 Release

How to Programmatically Convert Office Documents to PDF in Java

By Garry Klooesterman | 2025 Jul 31

Sanity Image
Read time

5 min

Summary: Sharing and archiving documents in a reliable format such as PDF is a must for businesses to operate efficiently. This blog covers converting Office file formats such as DOCX, XLSX, and PPTX to PDF easily and efficiently using the Apryse Office SDK in Java. 

Sharing and archiving documents in a reliable and universally accessible format is paramount. Portable Document Format (PDF) is a versatile and widely supported format that ensures consistent presentation across different platforms. Converting Office documents such as Word, Excel, and PowerPoint files into PDFs is a common requirement.

In this blog, we’ll walk through getting started with Office to PDF conversion in Java, then look at converting other Office file types. We’ll use the Apryse Office SDK for automated server-side PDF conversion for Office files in Java, but the code is available in other languages such as JavaScript, C#, Python, and more.

Why convert Word, Excel, and PowerPoint Files to PDF?

Copied to clipboard

Great question. Office file formats such as DOCX are commonplace today and are relatively easy to use and share as long as you have the right software. However, users can experience some common issues:

Why use the Apryse Office SDK?

Copied to clipboard

Besides conversion, the Apryse Office SDK has many features, enabling you to build powerful, robust apps with the features your clients need, such as:

Annotation: Choose from 35+ out-of-the-box annotations to add to your documents, including mark-ups and watermarks.

Digital Signatures: Add digital signature capabilities such as compliant signature fields, sign workflows, prompts, and styles to your documents. You can also use saved signatures to programmatically sign any number of documents.

Accessibility Compliance: Our modular WebViewer UI meets WCAG 2.2 AA standards, ensuring your applications are accessible to those with motor impairments, visual limitations, or cognitive disabilities without requiring additional development effort.

How do I convert Word documents to PDF using Java?

Copied to clipboard

This Apryse SDK Office to PDF conversion example is intended to show how specific documents in a hard-coded location can be converted to PDF.

The sample code uses the Apryse Office SDK Java library for converting DOCX, XLSX, PPTX to PDF without MS Office or any external dependencies. Office to PDF conversion in Java can be done on a Linux or Windows server, or in the user's web browser or mobile device.

  1. Go to our Apryse Server SDK landing page
  2. Select Java. For this project, I chose the Windows route, but you can choose the one you’re most comfortable with.
  3. Follow the steps for setting up JDK.
  4. Download the SDK.
  5. Extract the folder from the .zip file to your project folder.
  6. Get your Apryse trial key.

Now that we have the basics set up, we’ll look at the code from our OfficeToPDFTest sample. You’ll need to make some adjustments if you’re using this code as a base for your project, such as:

  • Adding your trial key.
  • Specifying the location of your file to convert.
  • Specifying the location to save the converted file.

Once you have your project ready, add the following code to your project .java file to perform the conversions. You can also see our documentation for the full code and more details.

Note: If you need help setting up your java project, follow these steps, which show how to set up everyone's favorite “Hello World” example.

//--------------------------------------------------------------------------------------- 
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved. 
// Consult legal.txt regarding legal and license information. 
//--------------------------------------------------------------------------------------- 

import com.pdftron.common.PDFNetException; 
import com.pdftron.pdf.Convert; 
import com.pdftron.pdf.DocumentConversion; 
import com.pdftron.pdf.PDFDoc; 
import com.pdftron.pdf.PDFNet; 
import com.pdftron.pdf.OfficeToPDFOptions; 
import com.pdftron.sdf.SDFDoc; 

//--------------------------------------------------------------------------------------- 
// The following sample illustrates how to use the PDF.Convert utility class to convert 
// MS Office files to PDF 
// 
// This conversion is performed entirely within the PDFNet and has no external or 
// system dependencies dependencies -- Conversion results will be the same whether // on Windows, Linux or Android. 
// 
// Please contact us if you have any questions. 
//---------------------------------------------------------------------------------------  

public class OfficeToPDFTest { 

static String input_path = "../../TestFiles/"; 
static String output_path = "../../TestFiles/Output/"; 
 
public static void main(String[] args) { 
    PDFNet.initialize(PDFTronLicense.Key()); 
    PDFNet.setResourcesPath("../../../Resources"); 
    PDFNet.terminate(); 
} 
 
public static void simpleDocxConvert(String inputFilename, String outputFilename) { 
    try (PDFDoc pdfdoc = new PDFDoc()) { 
 
        // perform the conversion with no optional parameters 
        Convert.officeToPdf(pdfdoc, input_path + inputFilename, null); 
 
        // save the result 
        pdfdoc.save(output_path + outputFilename, SDFDoc.SaveMode.INCREMENTAL, null); 
        // output PDF pdfdoc 
 
        // And we're done! 
        System.out.println("Done conversion " + output_path + outputFilename); 
    } catch (PDFNetException e) { 
        System.out.println("Unable to convert MS Office document, error:"); 
        e.printStackTrace(); 
        System.out.println(e); 
    } 
} 
}  

Let’s update those variables we talked about.

Change the input and output folders for your project with these lines code:

static String input_path = "../../TestFiles/";
static String output_path = "../../TestFiles/Output/";

Note: You don’t have to change the folders if you don’t want to. Just remember to update them if you do want to specify different folders for your project.

Add your license key in this line:

PDFNet.initialize(PDFTronLicense.Key());

After you’ve run the project, you should have successfully converted the Word document to PDF.

How do I convert Excel spreadsheets to PDF using Java?

Copied to clipboard

I’m glad you asked!

If you’re looking to convert other Office file types such as XLSX and PPTX to PDF, we'll use the same base code but make a few small adjustments based on the file type.

For example, if you’re using the sample code provided above, you can convert an Excel spreadsheet into a multiple page PDF with each page laid out in the same way as the original spreadsheet.

  1. Copy the file to the input folder.
  2. Change the inputFilename and outputFilename variables to match the file you want to convert.
Blog image

Figure 1: Variables to update for converting other file types.

The SDK is clever enough to know that .xlsx means that it needs to convert from Excel to PDF. The same goes for .pptx when converting PowerPoint files to PDF.

Blog image

Figure 2: An example Excel spreadsheet now converted to a PDF.

How to add additional font resources?

Copied to clipboard

The converter will try to use the fonts installed on the system, but you can also supply your own. See here for details on adding additional font resources.

FAQ

Copied to clipboard

Do I need Microsoft Office installed to use Apryse Office SDK?

No. Apryse Office SDK converts Office files without relying on Microsoft Office or any third-party software. It runs entirely in your Java environment.

What Office formats can I convert to PDF?

You can convert DOCX, XLSX, and PPTX to PDF.

Is the conversion consistent across platforms?

Yes. Since the SDK is independent of the OS, the conversion will be the same regardless of the OS you run it on.

Is this free to use?

You can get started with a free trial key. We also offer flexible licensing.

Is this SDK only for Java?

No. Apryse offers SDKs in JavaScript, Python, C#, and many other languages.

Conclusion

Copied to clipboard

Apryse offers a simple mechanism for converting Office documents to PDF without the need for Office to be installed. This can be done with just a few lines of code that use default options. More complex options exist to allow the conversion mechanism to be tailored to your requirements.

These powerful conversion capabilities, coupled with the ease of integration provided by its Java library, make the Apryse Office SDK the best choice for developers aiming to enhance their document processing workflows. Whether you're building a document management system, an online collaboration platform, or any other application involving Office documents, Apryse can help you provide a seamless and efficient conversion process.

See how this works in our live demo! You can also get started now or contact our sales team for any questions. Our Discord community is a great place for support and discussions.

 

Sanity Image

Garry Klooesterman

Senior Technical Content Creator

Share this post

email
linkedIn
twitter