How to Convert XLSX to PDF in Java
Using a Cloudmersive API, learn how to convert Excel files to PDF in Java
Join the DZone community and get the full member experience.
Join For FreePDF 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:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And add a reference to the dependency in pom.xml:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add it in your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
And add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
Once you have completed these initial steps, we can then call our function, ConvertDocumentXlsxToPdf:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentXlsxToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsxToPdf");
e.printStackTrace();
}
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:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentXlsToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentXlsToPdf");
e.printStackTrace();
}
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.
Opinions expressed by DZone contributors are their own.
Comments