In this blog tutorial, we are going to learn how to convert excel to pdf using java. For converting excel file into java, we are going to use this library called Apache POI. First, we will see the code and then we will go through it to understand it.
Dependencies required
For converting excel to PDF we need below dependency as part of the conversion and read write.
<!– https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml –>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!– https://mvnrepository.com/artifact/com.itextpdf/itextpdf –>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
Code how to convert excel to PDF using java
package com.slb.corejava;
import java.io.FileInputStream;
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class JavaExcelToPdfSLB {
public static void main(String[] args) throws Exception {
// Load the Excel file from C drive for example
FileInputStream input = new FileInputStream("C:\\Files\\input.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(input);
// Create a PDF document and store it in same directory as c and under files folder
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:\\Files\\output.pdf"));
document.open();
//Iterate over the Excel sheets and convert each to a Table in Pdf
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
// first, get first sheet of the document
XSSFSheet sheet = workbook.getSheetAt(i);
//from that sheet get first row. Create table in pdf.
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
//now iterate on sheet till we have last row and read the data and add in the table
for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {
XSSFRow row = sheet.getRow(j);
if (row != null) {
for (int k = 0; k < row.getLastCellNum(); k++) {
XSSFCell cell = row.getCell(k);
if (cell != null) {
table.addCell(cell.toString());
}
}
}
}
// add created table using document object
document.add(table);
System.out.println("created output pdf at given loc");
}
// Close the document and Excel input stream resources
document.close();
input.close();
}
}
Output:
created output pdf at given loc
Convert excel to pdf sample files
Input Excel for convert excel to PDF using java
Output generated pdf from excel:
Code explanation for converting excel to pdf in java
To convert Excel file to java we need two dependencies named apache poi and pdf and itextpdf. if you are using maven you can download the dependencies from maven central repo, if you are using standard core module then you can download the jars and use it in the build config to add to your classpath.
What is apache poi
Apache POI is an open source Java library that enables developers to read and write documents such as Word, Excel and PowerPoint.
POI reefers to “Poor Obfuscation Implementation” or “Plain Old Java Objects Interface, its API offers access to manipulate Office documents with Java code directly.
Developers use Apache POI programmatically open write modify and read Microsoft Office files programmatically using it.
What is itextpdf
iTextPDF is an open-source library written in Java that enables developers to programmatically create, edit and manipulate PDF documents.
It features an API which enables developers to generate PDFs from scratch as well as modify existing ones; creating documents compatible with multiple platforms (desktop or mobile device) as well as various PDF readers is made simple by this library.
The iTextPDF library is easy to use and comes with extensive documentation, making it a top choice for any developers to ues it.
It supports text formatting, tables, images and links as well as more advanced features like digital signatures and encryption – features developers often look for when selecting their PDF libraries.
Code Details for how to convert excel to PDF using java
Let’s walthrough with the code now. First we need to download the above two dependencies and the import it in the project via pom or jar.
Next step is to locate our file for reading, in this case we are storing excel file in c drive under folder named “files” and file name as “input.xlsx”.
Then first, we need to add below libraries/imports:
import java.io.FileInputStream;
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
After that, create an object of FileInputStream to open the input stream to Excel file as:
FileInputStream input = new FileInputStream(""C:\\Files\\input.xlsx");
We can now create an XSSFWorkbook object to read the Excel workbook:
XSSFWorkbook workbook = new XSSFWorkbook(input);
The XSSFWorkbook class is used to read Excel files in the XLSX format. If you are working with other or old version of Excel files in the XLS format, you will need to use the HSSFWorkbook class instead.
After that, we need to create a PDF doc using the itext PDF library. To create it, we can use Document object and passing it to a PdfWriter object:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:\\Files\\input.xlsx"));
document.open();
Here, in the above code snippet what we have done is that we created the object of document from itext lib then we get the instance of the file that we are going to create, and we have called method open on it to process via iteration on each cell and putting it in pdf table.
We pass the Document object and the path to the output PDF file, i.e. C drive Files folder and then filename to the PdfWriter.getInstance method.
//Iterate over the Excel sheets and convert each to a Table in Pdf
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
// first, get first sheet of the document
XSSFSheet sheet = workbook.getSheetAt(i);
//from that sheet get first row. Create table in pdf.
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
//now iterate on sheet till we have last row and read the data and add in the table
for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {
XSSFRow row = sheet.getRow(j);
if (row != null) {
for (int k = 0; k < row.getLastCellNum(); k++) {
XSSFCell cell = row.getCell(k);
if (cell != null) {
table.addCell(cell.toString());
}
}
}
}
// add created table using document object
document.add(table);
here, document.add(table) will add the table with contents onto the table on PDF.
We are using a for loop to iterate over each sheet of the workbook. For each sheet, we create a XSSFSheet object and a PdfPTable object. The PdfPTable class is used to represent a table in a PDF.
Then, We use another for loop to loop over each row in the sheet. For each row, we create a XSSFRow object and loop over each cell in the row. For each cell, we create a XSSFCell object and add its value to PDF table using the table.addCell method
At the last, we need to close the document and input stream.
document.close();
input.close();
These above close statements indicate that we are closing the resources properly. Note that we are not printing the last line of the Excel
Get whole code of the above example here at SLB-GITHUB
checkout more java tutorials here
Conclusion
In this tutorial of How to convert excel to PDF using java, we created a pdf document from excel document using two libraries named apache poi and iTextPdf. It is very easy with the help of these two libs to read and manipulate documents.