Convert Excel File to PDF in Java Example

Until now, we have been discussing in length on converting MS Excel documents to CSV files in Java. In this example, we are going to explain how to convert an Excel document (Office 97 – 2003 format) to PDF file in Java. Specifically, I’m interested in extracting an excel table data and create a PDF table data out of it. There are two key elements to this objective:
I have provided relevant hyperlinks to these two fragments, you can go through them if required. We will now write a Java program that combines these two key elements and provide the output we are looking for.

Input Excel Table Data:

A snapshot of the input XLS data is give below:

XLS to PDF in Java- Input Spreadsheet
XLS to PDF in Java- Input Spreadsheet



The objective is to transform this data to a PDF table data.

XLS to PDF – JAR Files


You would need the following Java libraries and associated JAR files for the program to work.
  • POI v3.8
  • iText v5.3.4

Excel to PDF – Java Program Example


The complete Java code that accepts Excel spreadsheet data as an input and transforms that to a PDF table data is provided below:
import java.io.FileInputStream;
import java.io.*;
//POI libraries to read Excel File
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import java.util.Iterator;
//itext libraries to write PDF file
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class excel2pdf {  
        public static void main(String[] args) throws Exception{
                //First we read the Excel file in binary format into FileInputStream
                FileInputStream input_document = new FileInputStream(new File("C:\\excel_to_pdf.xls"));
                // Read workbook into HSSFWorkbook
                HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
                // Read worksheet into HSSFSheet
                HSSFSheet 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("Excel2PDF_Output.pdf"));
                iText_xls_2_pdf.open();
                //we have two columns in the Excel sheet, so we create a PDF table with two columns
                //Note: There are ways to make this dynamic in nature, if you want to.
                PdfPTable my_table = new PdfPTable(2);
                //We will use the object below to dynamically add new data to the table
                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
                                                //you need to add more code here based on
                                                //your requirement / transformations
                                        case Cell.CELL_TYPE_STRING:
                                                //Push the data from Excel to PDF Cell
                                                 table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));
                                                 //feel free to move the code below to suit to your needs
                                                 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 xls
        }
}

Example Output


The Java program provided above creates a PDF file “Excel2PDF_Output.pdf” which will have your Excel sheet data. A screen dump of the PDF document produced by the code is given below:

Excel to PDF in Java - Output PDF Document
Excel to PDF in Java - Output PDF Document



That completes a very quick and basic tutorial to convert XLS table data into PDF table using Apache POI and iText, in Java. There is tremendous scope to customize the table to include styles etc. You can test this example and change it to suit to your needs. If you have a question on the functionality meanwhile, you can post it as a comment back to us. We will have a look.

14 comments:

  1. very clean n nice bit of code. it works. but, it does not copy the merged cell, styles, etc from excel file to pdf. how can i achieve it?

    ReplyDelete
  2. I am getting cast arguement my_table To Element in iText_xls_2_pdf.add( my_table);

    ReplyDelete
  3. @Anonymous, can you post your code with the version you are using?

    ReplyDelete
  4. how to make the whole content merge exactly into PDF?

    ReplyDelete
  5. @Anonymous, define "exactly" and show an example of your content.

    ReplyDelete
  6. @YellowRose: thanks for useful article.
    My question is: I want to convert chart excel to pdf, do itext and poi support?

    ReplyDelete
  7. Hi Atula, is it a chart created out of Excel or a chart object created via libraries like JFreechart? That defines the approach - also what is the type of the chart? (pie, line, bar?)

    ReplyDelete
  8. What about if the excel has empty cell.

    ReplyDelete
  9. how make number of columns dynamic

    ReplyDelete
  10. Facing the issue that from xlsx sheet not able to content reqding
    Please refer below code
    -----------------------------
    String contentType = message.getContentType();
    String attachFiles = "";
    // String saveDirectory = (String) resources.get(SystemRqstAppConstants.WebConstants.CUSTOMERITEMVENDORPO_PATH);
    String saveDirectory ="D:/ResumeFiles/";
    List errorsList= null;
    String messageContent = "";
    logger.info(":::::Timecards Auto Update before Attchments:::::");
    if (contentType.contains("multipart")) {
    // content may contain attachments
    String client="";
    if(subject.contains("PFIZER") || subject.contains("Pfizer") || subject.contains("pfizer"))
    client="Pfizer";
    else if(subject.contains("CSC") || subject.contains("Csc") || subject.contains("csc"))
    client="CSC";
    logger.info("Timecards Auto Update client name: "+client);
    Multipart multiPart = (Multipart) message.getContent();
    int numberOfParts = multiPart.getCount();
    for (int partCount = 0; partCount < numberOfParts; partCount++) {
    MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
    if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
    // this part is attachment
    fileName = part.getFileName();
    attachFiles += fileName + ", ";
    saveDirectory = saveDirectory+client+"/";
    File folder = new File(saveDirectory);
    if(!folder.exists())
    folder.mkdir();
    String destFilePath = saveDirectory+fileName;
    logger.info("Timecards Auto Update destFilePath: "+destFilePath);
    FileOutputStream fileOutStream = new FileOutputStream(destFilePath);
    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(destFilePath)));
    XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
    //save file
    FileOutputStream out = new FileOutputStream(destFilePath);
    wb.write(out);
    out.close();
    in.close();
    ----------------------------------
    Your reply really appreciate

    Thanks
    Rama

    ReplyDelete
  11. getting error for 'com.itextpdf.text.Element'

    ReplyDelete
  12. Hi,
    This code is only reading string elements. Integer data is not included in the pdf output.

    ReplyDelete
  13. How to convert XLSX to PDF with formatting of excel sheet cell styles?

    ReplyDelete
  14. Can we convert excel to pdf with cell formatting?

    ReplyDelete