DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Convert ODF Files to PDF in Java
  • How to Convert a PDF to Text (TXT) Using Java
  • How to Convert PDF to Text in Java

Trending

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents
  • Multi-Agent Orchestration on AWS With AgentCore Runtime and A2A
  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How to Convert XLSX to PDF in Java

How to Convert XLSX to PDF in Java

Using a Cloudmersive API, learn how to convert Excel files to PDF in Java

By 
Brian O'Neill
DZone Core CORE ·
Sep. 16, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

PDF documents are one of the most well-known and user-friendly formats for providing information to clients or associates. With its incredible versatility and compatibility, most platforms including PC, Mac, Linux, web browsers, and mobile platforms prefer this format for the sharing of locked documents and reports. Its convenience and maintenance of quality and fidelity make it the top choice for many organizations. 

The option to convert to PDF or print a spreadsheet from within your application is invaluable for your clients and will remove the worry of corrupted formatting that make occur in directly saving an Excel file. Furthermore, when working with complex information such as the data input in Excel Spreadsheets, one accidental key stroke in an unlocked document could cause extensive damage and possibly hours of work to find and fix the mistake. This problem is easily solved, however, by sharing your completed document as a PDF rather than an open spreadsheet, as it removes the danger of accidental editing. To help solve these issues, the following Convert API will provide you with an easy way to convert your Excel documents into PDF using Java.  

The following article will show you how to use this API to convert any XLSX or XLS file into a PDF, including a couple of example codes for you to test out for yourself. 

We will start with the most common Excel file format, XLSX. The first step to converting these documents with Java is to install our client software. For this, we will use Maven or Gradle. 

To install with Maven, add a reference to the repository in pom.xml: 

XML
x
 
1
<repositories> 
2
    <repository> 
3
        <id>jitpack.io</id> 
4
        <url>https://jitpack.io</url> 
5
    </repository> 
6
</repositories> 


And add a reference to the dependency in pom.xml:  

XML
xxxxxxxxxx
1
 
1
<dependencies> 
2
<dependency> 
3
    <groupId>com.github.Cloudmersive</groupId> 
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId> 
5
    <version>v3.54</version> 
6
</dependency> 
7
</dependencies> 


To install with Gradle, add it in your root build.gradle at the end of repositories:  

Java
xxxxxxxxxx
1
 
1
 allprojects {
2
    repositories {
3
        ...
4
        maven { url 'https://jitpack.io' }
5
    }
6
}


And add the dependency in build.gradle:  

Java
xxxxxxxxxx
1
 
1
dependencies {
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
3
}


Once you have completed these initial steps, we can then call our function, ConvertDocumentXlsxToPdf:  

Java
xxxxxxxxxx
1
24
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.ConvertDocumentApi;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    byte[] result = apiInstance.convertDocumentXlsxToPdf(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsxToPdf");
23
    e.printStackTrace();
24
}


The process for converting an older XLS document is similar to our previous example, with the same install steps for our repository client. After installation, we can once again call our function, ConvertDocumentXlsToPdf, as shown below:  

Java
xxxxxxxxxx
1
24
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.ConvertDocumentApi;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    byte[] result = apiInstance.convertDocumentXlsToPdf(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsToPdf");
23
    e.printStackTrace();
24
}


Your API Key, as mentioned above in the example code, can be accessed from Cloudmersive at no cost to you, ever. This option allows up to 800 calls across all available APIs; we can also provide higher levels of service scalable to your needs and software demands.

PDF Java (programming language) Convert (command)

Opinions expressed by DZone contributors are their own.

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Convert ODF Files to PDF in Java
  • How to Convert a PDF to Text (TXT) Using Java
  • How to Convert PDF to Text in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook