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.

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

Step 1: Load existing Excel spreadsheet in GcExcel workbook

Create a GcExcel workbook object and load an existing spreadsheet.

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

Workbook workbook=new Workbook();"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.

Following these steps will help you add these jar files as 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 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:


Step 3: Convert the spreadsheet to PDF

Save the spreadsheet to PDF using following code:"FinancialKPI.pdf", SaveFileFormat.Pdf);

You 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 on 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 in the thread below.

comments powered by Disqus