Skip to main content Skip to footer

Use a .NET Excel API to Convert XLSX Spreadsheets to PDFs in Java

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:

  • You need long-term preservation
  • You do not have Microsoft Office installed on your system but still want to print or distribute your Excel spreadsheets
  • You need to share your spreadsheet and keep your formatting. You need your Excel spreadsheets to look the same when opened on different systems

Additionally, you may need to create any of the following spreadsheets to include in a company report or to distribute to other departments:

  • Financial KPI
  • Company Budget
  • Sales Forecast
  • Invoices
  • Customer Profitability Analysis
  • Income Statement

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:

  1. Load Existing Excel Spreadsheet in GcExcel Workbook
  2. Add PDFBox as Library
  3. Add Page Setup Settings
  4. Convert the Spreadsheet to a PDF

Step 1: Load Existing Excel Spreadsheet in GcExcel Workbook

Create a GcExcel Java workbook object and load an existing FinancialKPIs.xlsx spreadsheet.

excel

Workbook workbook=new Workbook();   
workbook.open("FinancialKPI.xlsx");

Step 2: Add PDFBox as Library

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:

  1. Download package - PDFBox, FontBox, commons-loggings jar files from the PDFBox Apache site
  2. Under the 'src' folder in the project, add a folder 'libs'
  3. Copy the three jar files to 'libs' folder
  4. Right-click the’ libs' folder and choose the 'Add as library' option
  5. Press OK

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> 

Step 3: Add Page Setup Settings

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);

Step 4: Convert the Spreadsheet to a PDF

Save the spreadsheet to PDF using the following code:

workbook.save("FinancialKPIs.pdf");

 

Your PDF would look like this:

excel

You can set additional features and properties supported by saving your spreadsheet to PDF.

View the demo

If you have any questions or comments, please leave them below.

Tags:

comments powered by Disqus