In this tutorial we will see how to generate PDF from Excel in Java. This is very common requirement that using Java convert Excel to PDF and then display in some browser or return to some other application for printing. To perform this task we will simply load the Excel file and save it as PDF as described below.
Steps to Generate PDF from Excel in Java
- Add Aspose.Cells library to the project using Maven repository
- Add a reference to Workbook class using Import in the program
- Load the source Excel file into the Workbook object for conversion to PDF
- Save the Excel file as PDF
In these steps we see that using Java Excel to PDF conversion is simply performed by loading the target Excel file into Workbook class object and then saving it as PDF. For proper execution of this code, don’t forget to add the required library to your project from Maven and import the Workbook class in the program at the start.
Code to Generate PDF from Excel in Java
In this code we load the Excel file into Workbook class object and then save it as PDF using save() function. You can use PdfSaveOptions class object to set a number of properties before saving it as PDF file. Set the parameters and pass the PdfSaveOptions class object as second argument in the save() function. Note that no Interop or MS Excel is required to perform any of the above tasks.
In this article we learnt how using Java convert Excel to PDF however, if you want to generate an Excel file in Java, refer to the article Generate Excel file in Java.