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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • What Is mTLS? How To Implement It With Istio
  • Web Development Checklist
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices

Trending

  • What Is mTLS? How To Implement It With Istio
  • Web Development Checklist
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  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

Brian O'Neill user avatar by
Brian O'Neill
CORE ·
Sep. 16, 20 · Tutorial
Like (3)
Save
Tweet
Share
9.76K 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.

Trending

  • What Is mTLS? How To Implement It With Istio
  • Web Development Checklist
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: