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 other departments:
You can achieve 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.
Follow this topic to set up the project 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.
In this tutorial, we'll load an Excel spreadsheet in Java then convert it to a PDF using GcExcel Java using the following steps:
Create a GcExcel Java workbook object and load an existing FinancialKPIs.xlsx 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. You can find details about the specific versions of all the required GcExcel Java dependencies for various features in the help topic GcExcel Dependencies.
Following these steps will help you add these jar files as a 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, adding GcExcel Java dependency would be sufficient. Maven or Gradle will download and install all dependent jars automatically.
In some versions of Maven, you may get a dependency error on adding above jar files. To resolve that, please add the 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>
If you need to print the worksheet on a single PDF, you can set additional options through the PageSetup class of the worksheet.
worksheet.getPageSetup().setOrientation(PageOrientation.Landscape);
worksheet.getPageSetup().setIsPercentScale(false);
worksheet.getPageSetup().setFitToPagesWide(1);
worksheet.getPageSetup().setFitToPagesTall(1);
Save the spreadsheet to PDF using the following code:
workbook.save("FinancialKPIs.pdf");
Your PDF would look like this:
You can set additional features and properties supported by saving your spreadsheet to PDF.
If you have any questions or comments, please leave them below.