
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 API - Installation
- Convert Excel XLS/XLSX to PDF using Java
- Convert Excel to PDF with Particular Compliance using Java
- Render One PDF Page Per Sheet using Java
- Convert a Range of Sheets to PDF using Java
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:
- Load the workbook using the Workbook class.
- Save the workbook as a PDF file with the Workbook.save method, specifying the
SaveFormat.PDFconstant.
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:
- Initialise a Workbook object with the source Excel file.
- Create a PdfSaveOptions instance.
- Set the compliance using
PdfSaveOptions.setCompliance(PdfCompliance). - 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.
- Load the workbook.
- Create a PdfSaveOptions object.
- Call
PdfSaveOptions.setOnePagePerSheet(true). - 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.
- Open the workbook.
- Configure PdfSaveOptions with the desired page index and count.
- 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.