Skip to main content Skip to footer

Use an Excel API to Convert Spreadsheets to PDFs in Java Apps

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 exactly 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 different departments:

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

You can achieve all of these objectives by exporting your spreadsheet to a PDF.

Document Solutions for Excel, Java Edition (DsExcel Java), previously 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 DsExcel Java. The steps of adding the package and working with DsExcel 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 the existing Excel spreadsheet in the DsExcel workbook

2. Add PDFBox as a library

3. Convert the spreadsheet to PDF

Step 1: Load the existing Excel spreadsheet in the DsExcel workbook

Create a DsExcel workbook object and load an existing spreadsheet.

Use an Excel API to Convert Spreadsheets to PDFs in Java Apps

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

Step 2: Add PDFBox as library

DsExcel Java references PDFBox to convert spreadsheets to PDF. PDFBox also depends on FontBox and Commons Logging packages.

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.
  2. Under the 'src' folder in the project, add a folder 'libs'.
  3. Copy the three jar files to 'libs' folder.
  4. Right-click 'libs' folder and choose '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, just adding a dependency of DsExcel Java 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 the 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: Convert the spreadsheet to PDF

Save the spreadsheet to PDF using the following code:

workbook.save("FinancialKPI.pdf", SaveFileFormat.Pdf);

Your PDF would look like this:

Use an Excel API to Convert Spreadsheets to PDFs in Java Apps Use an Excel API to Convert Spreadsheets to PDFs in Java Apps

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

  • Set Line properties

    • Line Width
    • Line Color
    • Line DashArray
    • Line Position
    • Line Cap
  • Set Text properties

    • Custom Font
    • Bold
    • Italic
    • MeasureString
    • Font Size
    • Font Color
    • Text Position
    • Annotation
  • Set Fill

    • Solid
  • Images

  • Set Page
  • Set Clip Region

  • Export

    • Text
    • Number formats
    • Overflow text
    • Font effects
    • Borders
    • Conditional Formatting
    • Picture
    • Fills
    • Sparklines
    • Table
  • Save workbook to PDF

  • Save worksheet to PDF
  • Set font's folder

View the demo

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

comments powered by Disqus