Create PDF Table from XLSX File Java Example

In the last example, we observed how to convert an Excel document in Office 97-2003 binary format, to PDF table using POI and iText in Java. I would like to extend that example to this tutorial, in which we will describe creating a PDF file from XLSX data (open office format). POI offers methods to process XLSX documents and we will wrap the information that is read from XLSX using POI to a PDF table in iText

We have written so many tutorials about iText and POI, so I will not go more on the basics. If you are keen to understand how to work with POI, you can refer to the related post section at the bottom of this tutorial.

XLSX to PDF - POI / iText / Java Example


Full Java program to convert XLSX (Excel 2007 / 2012 ) data to PDF is provided below:

import java.io.FileInputStream;
import java.io.*;
//POI libraries to read Excel 2007 format file
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.ss.usermodel.*;
import java.util.Iterator;
//itext libraries to create PDF table from XLSX
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class xlsx2pdf {  
        public static void main(String[] args) throws Exception{
                //First we read the XLSX in binary format into FileInputStream
                FileInputStream input_document = new FileInputStream(new File("C:\\excel_to_pdf.xlsx"));
                // Read workbook into XSSFWorkbook
                XSSFWorkbook my_xls_workbook = new XSSFWorkbook(input_document); 
                // Read worksheet into XSSFSheet
                XSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0); 
                // To iterate over the rows
                Iterator<Row> rowIterator = my_worksheet.iterator();
                //We will create output PDF document objects at this point
                Document iText_xls_2_pdf = new Document();
                PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("PDFOutput.pdf"));
                iText_xls_2_pdf.open();
                //we have two columns in the Excel sheet, so we create a PDF table with two columns                
                PdfPTable my_table = new PdfPTable(2);
                //cell object to capture data
                PdfPCell table_cell;
                //Loop through rows.
                while(rowIterator.hasNext()) {
                        Row row = rowIterator.next(); 
                        Iterator<Cell> cellIterator = row.cellIterator();
                                while(cellIterator.hasNext()) {
                                        Cell cell = cellIterator.next(); //Fetch CELL
                                        switch(cell.getCellType()) { //Identify CELL type
                                                
                                        case Cell.CELL_TYPE_STRING:
                                                //Push the data from Excel to PDF Cell
                                                 table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));                                                
                                                 my_table.addCell(table_cell);
                                                break;
                                        }
                                        //next line
                                }
                
                }
                //Finally add the table to PDF document
                iText_xls_2_pdf.add(my_table);                       
                iText_xls_2_pdf.close();                
                //we created our pdf file..
                input_document.close(); //close xlsx
        }
}

To compile and run this code, you need a number of JAR files. These are supplied with POI and iText libraries. A sample compile / execute command is provided below:
javac -classpath .;poi-3.8.jar;opencsv-2.3.jar;poi-ooxml-3.8.jar;p
oi-ooxml-schemas-3.8.jar;itextpdf-5.3.4.jar xlsx2pdf.java

java -classpath .;poi-3.8.jar;opencsv-2.3.jar;poi-ooxml-3.8.jar;po
i-ooxml-schemas-3.8.jar;dom4j-1.6.1.jar;xmlbeans-2.3.0.jar;itextpdf-5.3.4.jar xl
sx2pdf

4 comments:

  1. Document iText_xls_2_pdf = new Document(); this line is showing an error!! what library to use for this?

    ReplyDelete
  2. Document iText_xls_2_pdf = new Document(); this line is showing an error!! what library to use for this?

    ReplyDelete
  3. The type Document is from package com.itextpdf.text.
    import com.itextpdf.text.Document

    ReplyDelete
  4. com.itextpdf.text.Document pdf = new com.itextpdf.text.Document(PageSize.A4.rotate());
    PdfWriter.getInstance((com.itextpdf.text.Document) pdf, new FileOutputStream("Excel2PDF_Output.pdf"));

    write it

    ReplyDelete