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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Docker Base Images Demystified: A Practical Guide
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  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 user avatar
Brian O'Neill
DZone Core CORE ·
Sep. 16, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.5K Views

Join the DZone community and get the full member experience.

Join For Free

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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!