Are you using an Excel API to generate spreadsheets in Java applications? When you are working with data, there will be times when you do not want to store your data in an Excel spreadsheet. Instead, you need a PDF.
There are several reasons why you wouldn't want to store data in an Excel spreadsheet:
Additionally, you may need to create any of the following spreadsheets to include in a company report, or to distribute to different departments:
You can achieve all of these objectives by exporting your spreadsheet to a PDF.
GrapeCity Documents for Excel, Java Edition (GcExcel Java) is a high-speed, small-footprint spreadsheet API that requires zero dependencies on Excel. With full support on Windows, Linux, and MAC, you can generate, load, modify, and save spreadsheets, then convert them to a PDF.
In this article, we demonstrate how to setup the project in a Java IDE using GcExcel Java. The steps of adding the package and working with GcExcel Java are the same on all three operating systems - Windows, MAC, and Linux.
Here are the three steps for converting spreadsheets to PDFs in Java applications:
1. Load existing Excel spreadsheet in GcExcel workbook
2. Add PDFBox as a library
3. Convert the spreadsheet to PDF
Create a GcExcel workbook object and load an existing spreadsheet.
Workbook workbook=new Workbook(); workbook.open("FinancialKPI.xlsx");
GcExcel Java references PDFBox to convert spreadsheet to PDF. PDFBox also depends on FontBox and Commons Logging packages.
Following these steps will help you add these jar files as library to your project:
Note: The above steps would be required only if the project is an original Java console project. If the project is a Maven or Gradle project, just adding dependency of GcExcel Java would be sufficient. Maven or Gradle will download and install all dependent jars automatically.
In some versions of Maven, you may get dependency error on adding above jar files. To resolve that, please add following node before the dependencies in pom.xml:
<build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> </plugin> </plugins> </build>
Save the spreadsheet to PDF using following code:
You PDF would look like this:
You can set additional features and properties which are supported on saving your spreadsheet to PDF.
Set Line properties
Set Text properties
Set Clip Region
Save workbook to PDF
If you have any questions or comments, please leave in the thread below.