Convert Excel to PDF Java

Excel to PDF conversion is a common requirement when you need to share spreadsheet data in a fixed‑layout format. This guide shows how to convert Excel files to PDF using Java, enabling you to automate XLS/XLSX to PDF (or PDF/A) conversion in web, desktop, or server‑side applications.

Java Excel to PDF Converter - Free Download

Aspose.Cells for Java is a powerful spreadsheet processing library that lets you create, edit, and convert Excel files (XLS, XLSX, CSV, etc.) with high fidelity. With just a few lines of code, you can generate PDF, PDF/A, or other document formats. Download the library as a JAR or add it to your project via Maven.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>20.7</version>
</dependency>

Convert Excel XLS to PDF in Java

The basic workflow for converting an Excel workbook to PDF consists of two steps:

  1. Load the workbook using the Workbook class.
  2. Save the workbook as a PDF file with the Workbook.save method, specifying the SaveFormat.PDF constant.

The code snippet below demonstrates how to convert an XLSX file to PDF in Java.

Java Save Excel XLS as PDF - Set PDF Compliance

PDF files can be created to meet specific compliance standards such as PDF/A‑1a, PDF/A‑2b, etc. To produce a compliant PDF, use the PdfSaveOptions class and set the desired compliance level before saving.

Steps:

  1. Initialise a Workbook object with the source Excel file.
  2. Create a PdfSaveOptions instance.
  3. Set the compliance using PdfSaveOptions.setCompliance(PdfCompliance).
  4. Save the workbook with the configured options.

The following example converts an XLSX file to a PDF/A‑2b document.

Export Excel XLSX to PDF in Java - One Page Per Sheet

By default, each worksheet may span multiple PDF pages based on its print settings. If you prefer each sheet to appear on a single PDF page, enable the OnePagePerSheet option.

  1. Load the workbook.
  2. Create a PdfSaveOptions object.
  3. Call PdfSaveOptions.setOnePagePerSheet(true).
  4. Save the workbook as PDF.

The code below shows how to generate a PDF where every worksheet is rendered on one page.

Java Excel to PDF - Export Range of Sheets to PDF

Sometimes you only need a subset of worksheets in the final PDF. Use PdfSaveOptions.setPageIndex and PdfSaveOptions.setPageCount to specify the start sheet and the number of sheets to include.

  1. Open the workbook.
  2. Configure PdfSaveOptions with the desired page index and count.
  3. Save the selected range as PDF.

The following snippet converts a specific range of sheets to PDF.

Conclusion

You now have a complete set of examples for converting Excel workbooks to PDF using Java and Aspose.Cells. Whether you need simple conversion, PDF/A compliance, single‑page per sheet output, or selective sheet ranges, the library provides straightforward APIs to meet your requirements.

Excel to PDF Java Converter - Read More

Aspose.Cells for Java offers many additional features such as chart rendering, formula calculation, and workbook protection. Explore the full capabilities in the official documentation.

See Also