Apache poi excel to pdf

Add on to assylias’s answer

The code from assylias above was very helpful to me in solving this problem. The answer from santhosh could be great if you don’t care about the resulting PDF looking exactly like your excel pdf export would look. However, if you are, say, filling out an excel template using Apache POI an then trying to export that while preserving its look and not writing a ton of code in iText just to try to get close to that look, then the VBS option is quite nice.

I’ll share a Java version of the kotlin assylias has above in case that helps anyone. All credit to assylias for the general form of the solution.

In Java:

try {
    //create a temporary file and grab the path for it
    Path tempScript = Files.createTempFile("script", ".vbs");

    //read all the lines of the .vbs script into memory as a list
    //here we pull from the resources of a Gradle build, where the vbs script is stored
    System.out.println("Path for vbs script is: '" + Main.class.getResource("xl2pdf.vbs").toString().substring(6) + "'");
    List<String> script = Files.readAllLines(Paths.get(Main.class.getResource("xl2pdf.vbs").toString().substring(6)));

    // append test.xlsm for file name. savePath was passed to this function
    String templateFile = savePath + "\test.xlsm";
    templateFile = templateFile.replace("\", "\\");
    String pdfFile = savePath + "\test.pdf";
    pdfFile = pdfFile.replace("\", "\\");
    System.out.println("templateFile is: " + templateFile);
    System.out.println("pdfFile is: " + pdfFile);

    //replace the placeholders in the vbs script with the chosen file paths
    for (int i = 0; i < script.size(); i++) {
        script.set(i, script.get(i).replaceAll("XL_FILE", templateFile));
        script.set(i, script.get(i).replaceAll("PDF_FILE", pdfFile));
        System.out.println("Line " + i + " is: " + script.get(i));
    }

    //write the modified code to the temporary script
    Files.write(tempScript, script);

    //create a processBuilder for starting an operating system process
    ProcessBuilder pb = new ProcessBuilder("wscript", tempScript.toString());

    //start the process on the operating system
    Process process = pb.start();

    //tell the process how long to wait for timeout
    Boolean success = process.waitFor(timeout, minutes);
    if(!success) {
        System.out.println("Error: Could not print PDF within " + timeout + minutes);
    } else {
        System.out.println("Process to run visual basic script for pdf conversion succeeded.");
    }
    
} catch (Exception e) {
    e.printStackTrace();
    Alert saveAsPdfAlert = new Alert(AlertType.ERROR);
    saveAsPdfAlert.setTitle("ERROR: Error converting to pdf.");
    saveAsPdfAlert.setHeaderText("Exception message is:");
    saveAsPdfAlert.setContentText(e.getMessage());
    saveAsPdfAlert.showAndWait();  
}

VBS:

Option Explicit
Dim objExcel, strExcelPath, objSheet

strExcelPath = "XL_FILE"


Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Open strExcelPath
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

objSheet.ExportAsFixedFormat 0, "PDF_FILE",0, 1, 0, , , 0

objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

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.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import java.util.Iterator;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class excel2pdf {  
        public static void main(String[] args) throws Exception{
                
                FileInputStream input_document = new FileInputStream(new File("C:\excel_to_pdf.xls"));
                
                HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
                
                HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0); 
                
                Iterator<Row> rowIterator = my_worksheet.iterator();
                
                Document iText_xls_2_pdf = new Document();
                PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("Excel2PDF_Output.pdf"));
                iText_xls_2_pdf.open();
                
                
                PdfPTable my_table = new PdfPTable(2);
                
                PdfPCell table_cell;
                
                while(rowIterator.hasNext()) {
                        Row row = rowIterator.next(); 
                        Iterator<Cell> cellIterator = row.cellIterator();
                                while(cellIterator.hasNext()) {
                                        Cell cell = cellIterator.next(); 
                                        switch(cell.getCellType()) { 
                                                
                                                
                                        case Cell.CELL_TYPE_STRING:
                                                
                                                 table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));
                                                 
                                                 my_table.addCell(table_cell);
                                                break;
                                        }
                                        
                                }
                
                }
                
                iText_xls_2_pdf.add(my_table);                       
                iText_xls_2_pdf.close();                
                
                input_document.close(); 
        }
}

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.

/* Copyright 2016 nakazawaken1 Licensed under the Apache License, Version 2.0 (the «License»); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an «AS IS» BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package jp.qpg; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Objects; import java.util.Optional; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.logging.Logger; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSimpleShape; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSimpleShape; /** * Convert Excel to pdf or text * * @author nakazawaken1 */ public class ExcelTo { /** * logger */ private static final Logger logger = Logger.getLogger(ExcelTo.class.getCanonicalName()); /** * excel to pdf * * @param book excel workbook * @param out output to pdf * @param documentSetup document setup * @throws IOException I/O exception */ public static void pdf(Workbook book, OutputStream out, Consumer<PDFPrinter> documentSetup) throws IOException { Objects.requireNonNull(book); Objects.requireNonNull(out); try (PDFPrinter printer = new PDFPrinter()) { printer.documentSetup = Optional.ofNullable(documentSetup); for (int i = 0, end = book.getNumberOfSheets(); i < end; i++) { Sheet sheet = book.getSheetAt(i); int rowCount = sheet.getPhysicalNumberOfRows(); if (rowCount <= 0) { logger.info(sheet.getSheetName() + «: empty»); continue; /* skip blank sheet */ } logger.info(sheet.getSheetName() + «: « + rowCount + » rows»); printer.println(«sheet name: « + sheet.getSheetName()); printer.println(«max row index: « + sheet.getLastRowNum()); printer.println(«max column index: « + Tool.stream(sheet.rowIterator(), rowCount).mapToInt(Row::getLastCellNum).max().orElse(0)); eachCell(sheet, (cell, range) -> Tool.cellValue(cell).ifPresent( value -> printer.println(‘[‘ + (range == null ? new CellReference(cell).formatAsString() : range.formatAsString()) + «] « + value))); eachShape(sheet, shapeText(text -> printer.println(«[shape text] « + text))); printer.newPage(); } printer.getDocument().save(out); } } /** * get shape text * * @param consumer text consumer * @return binary consumer */ public static BiConsumer<XSSFSimpleShape, HSSFSimpleShape> shapeText(Consumer<String> consumer) { return (shapeX, shapeH) -> { try { consumer.accept(shapeX != null ? shapeX.getText() : shapeH.getString().getString()); } catch (NullPointerException e) { /* NullPointerException occurs depending on shape type */ } }; } /** * excel to text * * @param book excel workbook * @param out output to text */ public static void text(Workbook book, OutputStream out) { Objects.requireNonNull(book); Objects.requireNonNull(out); try (PrintStream printer = Try.to(() -> new PrintStream(out, true, System.getProperty(«file.encoding»))).get()) { for (int i = 0, end = book.getNumberOfSheets(); i < end; i++) { Sheet sheet = book.getSheetAt(i); int rowCount = sheet.getPhysicalNumberOfRows(); if (rowCount <= 0) { logger.info(sheet.getSheetName() + «: empty»); continue; /* skip blank sheet */ } logger.info(sheet.getSheetName() + «: « + rowCount + » rows»); printer.println(«sheet name: « + sheet.getSheetName()); printer.println(«max row index: « + sheet.getLastRowNum()); printer.println(«max column index: « + Tool.stream(sheet.rowIterator(), rowCount).mapToInt(Row::getLastCellNum).max().orElse(0)); eachCell(sheet, (cell, range) -> Tool.cellValue(cell).ifPresent( value -> printer.println(‘[‘ + (range == null ? new CellReference(cell).formatAsString() : range.formatAsString()) + «] « + value))); sheet.getCellComments().entrySet().forEach(entry -> { printer.println(«[comment « + entry.getKey() + «] « + entry.getValue().getString()); }); eachShape(sheet, shapeText(text -> printer.println(«[shape text] « + text))); printer.println(«———«); } } } /** * traverse all cells * * @param sheet sheet * @param consumer cell consumer */ public static void eachCell(Sheet sheet, BiConsumer<Cell, CellRangeAddress> consumer) { sheet.rowIterator().forEachRemaining(row -> { row.cellIterator().forEachRemaining(cell -> { int rowIndex = cell.getRowIndex(); int columnIndex = cell.getColumnIndex(); boolean until = true; for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) { if (mergedRegion.isInRange(rowIndex, columnIndex)) { if (rowIndex == mergedRegion.getFirstRow() && columnIndex == mergedRegion.getFirstColumn()) { consumer.accept(cell, mergedRegion); } until = false; break; } } if (until) { consumer.accept(cell, null); } }); }); } /** * traverse all shape * * @param sheet sheet * @param consumer shape consumer */ public static void eachShape(Sheet sheet, BiConsumer<XSSFSimpleShape, HSSFSimpleShape> consumer) { if (sheet instanceof XSSFSheet) { Optional.ofNullable(((XSSFSheet) sheet).getDrawingPatriarch()).ifPresent(drawing -> drawing.getShapes().forEach(shape -> { if (shape instanceof XSSFSimpleShape) { consumer.accept((XSSFSimpleShape) shape, null); } })); } else if (sheet instanceof HSSFSheet) { Optional.ofNullable(((HSSFSheet) sheet).getDrawingPatriarch()).ifPresent(drawing -> drawing.getChildren().forEach(shape -> { if (shape instanceof HSSFSimpleShape) { consumer.accept(null, (HSSFSimpleShape) shape); } })); } } /** * command * * @param args [-p password] [-m true|false(draw margin line if true)] Excel files(.xls, .xlsx, .xlsm) */ public static void main(String[] args) { Objects.requireNonNull(args); int count = 0; boolean[] drawMarginLine = { false }; for (int i = 0; i < args.length; i++) { switch (args[i]) { case «-m»:/* set draw margin line */ i++; drawMarginLine[0] = Boolean.parseBoolean(args[i]); break; case «-p»:/* set password */ i++; Biff8EncryptionKey.setCurrentUserPassword(args[i]); break; default: String path = Tool.trim(args[i], «»», «»»); String toPath = Tool.changeExtension(path, «.pdf»); String toTextPath = Tool.changeExtension(path, «.txt»); try (InputStream in = Files.newInputStream(Paths.get(path)); Workbook book = WorkbookFactory.create(in); OutputStream out = Files.newOutputStream(Paths.get(toPath)); OutputStream outText = Files.newOutputStream(Paths.get(toTextPath))) { logger.info(«processing: « + path); pdf(book, out, printer -> { printer.setPageSize(PDRectangle.A4, false); printer.setFontSize(10.5f); printer.setMargin(15); printer.setLineSpace(5); printer.setDrawMarginLine(drawMarginLine[0]); }); text(book, outText); logger.info(«converted: « + toPath + «, « + toTextPath); count++; } catch (IOException e) { throw new UncheckedIOException(e); } break; } } logger.info(«processed « + count + » files.»); } }
 Hi,

This is an example article for how to read excel
file both (.xlsx and .xls) and convert the PDF document with table of contents.
I hope this tutorial will help for the beginners who are interested in to use
Apache POI with iText PDF.

Here I have used the Apache POI for reading the
excel file because it support to read both .xlsx and .xls files and used iText
PDF for to create PDF document.

Table of Content

1.    Apache POI Introduction.

1.1 Use of HSSF and XSSF.

2.    iText PDF Introduction.

3.    Requirements.

4.    Adding Libraries into Build Path.

4.1         Configure Build Path in Eclipse

4.2         Add External Jars in Eclipse

5.    Step by Step Explanation about Source Code.

5.1 Import Libraries

5.2 Entry Point

5.3 Load Spreadsheet

5.4 Get File Extension

5.5 Read Spreadsheet

5.6 Add Meta data in the PDF document

5.7 Add Title Page in the PDF document

5.8 Check empty cell in the Excel and create empty cell in the PDF document Table.

6.    Conclusion.

1.   
Apache
POI Introduction:

            Apache POI, a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project, provides pure Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint and Excel.

The Apache POI project contains the
following subcomponents:

·        
POIFS (Poor Obfuscation Implementation File System) – This
component reads and writes Microsoft’s OLE2 Compound document. Since
all Microsoft Office files are OLE2 files, this component is the
basic building block of all the other POI elements. POIFS can therefore be used
to read a wider variety of files, beyond those whose explicit decoders are
already written in POI.


·        
HSSF (Horrible Spreadsheet Format) – reads and
writes Microsoft Excel (XLS) format files. It can read files written
by Excel 97 onwards; this file format is known as the BIFF
8
 format. As the Excel file format is complex and contains a number of
tricky characteristics, some of the more advanced features cannot be read.

·        
XSSF (XML Spreadsheet Format) – reads and writes Office Open
XML (XLSX) format files. Similar feature set to HSSF, but for Office Open
XML files.

·        
HPSF (Horrible Property Set Format) – reads «Document
Summary» information from Microsoft Office files. This is
essentially the information that one can see by using the File|Propertiesmenu
item within an Office application.

·        
HWPF (Horrible Word Processor Format) – aims to read and
write Microsoft Word 97 (DOC) format files. This component is in
initial stages of development.

·        
HSLF (Horrible Slide Layout Format) – a pure Java implementation
for Microsoft Power Point files. This provides the ability to read,
create and edit presentations (though some things are easier to do than others)

·        
HDGF (Horrible Diagram Format) – an initial pure Java
implementation for Microsoft Visio binary files. It provides an
ability to read the low level contents of the files.

·        
HPBF (Horrible Publisher Format) – a pure Java implementation for
Microsoft Publisher files.


·        
HSMF (Horrible Stupid Mail Format) – a pure Java implementation
for Microsoft Outlook MSG files.

·        
DDF (Dreadful Drawing Format) – a package for decoding the
Microsoft Office Drawing format.

1.1 Use of HSSF and XSSF.

For reading excel we can use any one of HSSF or
XSSF, but both are excel version based. 
In my example I have used both HSSF and XSSF with SS Model (SS = HSSF +
XSSF) for to read both .xlsx and .xls file. Here below I have mentioned the use
of both XSSF and HSSF model.

HSSF and XSSF provide ways to read spreadsheets create, modify,
read and write XLS spreadsheets. They provide:


  • Low
    level structures for those with special needs
  • An
    Event model API for efficient read-only access
  • User
    model API for creating, reading and modifying XLS files.

2. iText
PDF Introduction

In my example I have used
the iText API for to create the PDF document and make the table based on spreadsheet
content. Because iText API provides an direct way to make an document with Paragraphs,
Tables and Fonts and easy to understand.

Both Apache POI and iText
are Open source, so we don’t want to pay cost for to use those APIs.

3.
Requirements:


1.    JDK 1.4 or
Later

2.    Apache
POI- 3.9.

3.    iText PDF
– 5.1.0

4.    dom4j-1.6.1

5.    xmlbeans-2.3.0

I have attached source code
in end of the tutorial. Download those and run it.

4. Adding Libraries into Build Path

Do the below steps in
eclipse to add Jars into Project build Path.

Step1: Configure Build Path in Eclipse

      
Right click the project folder and choose the Build Path -> Configure
Build Path. Like below,

Step2: Add External Jars in Eclipse

     Click
the Libraries tab. Click the Add External Jar button to add Jar once done this.
Go to the Order and Export tab and choose Select All then click ok.

6.    Step by Step Explanation about Source Code

Step 1: Import Libraries

      Import
the Libraries, if you are using eclipse IDE then it will automatically imported
when you are using those.

import
java.io.File;

import
java.io.FileInputStream;

import
java.io.FileNotFoundException;

import
java.io.FileOutputStream;

import java.io.IOException;

import
java.io.InputStream;

import
java.util.Date;

import
java.util.Iterator;

import
org.apache.poi.hssf.usermodel.HSSFWorkbook;

import
org.apache.poi.openxml4j.opc.OPCPackage;

import
org.apache.poi.ss.usermodel.Cell;

import
org.apache.poi.ss.usermodel.Row;

import
org.apache.poi.ss.usermodel.Sheet;

import
org.apache.poi.ss.usermodel.Workbook;

import
org.apache.poi.xssf.usermodel.XSSFWorkbook;

import
com.itextpdf.text.Anchor;

import
com.itextpdf.text.BaseColor;

import
com.itextpdf.text.Chapter;

import
com.itextpdf.text.Document;

import
com.itextpdf.text.DocumentException;

import
com.itextpdf.text.Element;

import
com.itextpdf.text.Font;

import
com.itextpdf.text.Paragraph;

import
com.itextpdf.text.Phrase;

import
com.itextpdf.text.Section;

import
com.itextpdf.text.pdf.PdfPCell;

import
com.itextpdf.text.pdf.PdfPTable;

import com.itextpdf.text.pdf.PdfWriter;

The above libraries I have used in the example application for to
read excel and make PDF.

Step 2: Entry Point

            Main method is the entry point of this example. Here I
have hardcoded the inputs excel file. Change the path to refer your excel file.

 public static void main(String[] args) {

                        String
excelFile =
«E:\Uvaraj\newarticle\example.xlsx»;

                        File
xlsFile =
new File(excelFile);

                        Workbook
workbook;

                        try {

                                    workbook
= loadSpreadSheet(xlsFile);

                                    readSpreadSheet(workbook);

                        } catch
(FileNotFoundException e) {

                                    System.err.println(«Excel File (or) PDF File is already opened.
Please close the file»
);

                                    System.exit(1);

                        } catch
(Exception e) {

                                    e.printStackTrace();

                        }

            }

Step 3:
Load Spreadsheet

loadSpreadSheet (xlsFile)
is used for to load the excel file. Based on file extension I have created the
workbook. See the section 1.1 Use of HSSF and XSSF for details.

private static
Workbook loadSpreadSheet(File xlsFile)
throws Exception {

                        Workbook
workbook =
null;

                        String
ext = getFileExtension(xlsFile.getName());

                        if
(ext.equalsIgnoreCase(
«xlsx»)) {

                                    OPCPackage
pkg = OPCPackage.open(xlsFile.getAbsolutePath());

                                    workbook
=
new XSSFWorkbook(pkg);

                                    pkg.close();

                        } else if
(ext.equalsIgnoreCase(
«xls»)) {

                                    InputStream
xlsFIS =
new FileInputStream(xlsFile);

                                    workbook
=
new HSSFWorkbook(xlsFIS);

                                    xlsFIS.close();

                        } else {

                                    throw new Exception(«FILE EXTENSION NOT RECOGNIZED»);

                        }

                        return
workbook;

            }

Step 4: Get File Extension

getExtension (xlsFile.getName ()) is used for to get the
extension of the file.

private static
String getFileExtension(String fileName) {

                        String
ext =
«»;

                        int mid = fileName.lastIndexOf(«.»);

                        ext =
fileName.substring(mid + 1, fileName.length());

                        System.out.println(«File Extension —« + ext);

                        return
ext;

            }

Step 5:
Read Spreadsheet

readSpreadSheet(workbook)
used to read the excel file and create the PDF document with table.

 private static void readSpreadSheet(Workbook workbook) throws
IOException,

                                    DocumentException
{

                        Document
document =
new Document();

                        PdfWriter.getInstance(document,
new FileOutputStream(FILE));

                        document.open();

                        addMetaData(document);

                        addTitlePage(document);

                        Anchor
anchor =
new Anchor(«First
Chapter»
, catFont);

                        anchor.setName(«First Chapter»);

                        // Second parameter is the number of the chapter

                        Chapter
catPart =
new Chapter(new Paragraph(anchor), 1);

                        Paragraph
subPara =
new Paragraph(«Table», subFont);

                        Section
subCatPart = catPart.addSection(subPara);

                        addEmptyLine(subPara,
5);

                        Sheet
sheet = workbook.getSheetAt(0);

                        // Iterate through each rows from first sheet

                        Iterator<Row>
rowIterator = sheet.iterator();

                        int temp = 0;

                        boolean
flag =
true;

                        PdfPTable
table =
null;

                        while
(rowIterator.hasNext()) {

                                    Row
row = rowIterator.next();

                                    int cellNumber =
0;

                                    if (flag) {

                                                table
=
new PdfPTable(row.getLastCellNum());

                                                flag
=
false;

                                    }

                                    // For each row, iterate through each columns

                                    Iterator<Cell>
cellIterator = row.cellIterator();

                                    while
(cellIterator.hasNext()) {

                                                Cell
cell = cellIterator.next();

                                                switch
(cell.getCellType()) {  

                                                case Cell.CELL_TYPE_STRING:

                                                            if (temp == 0)
{                                 

                                                                        numberOfColumns
= row.getLastCellNum();

                                                                        PdfPCell
c1 =
new PdfPCell(new Phrase(

                                                                                                cell.getStringCellValue()));

                                                                        c1.setHorizontalAlignment(Element.ALIGN_CENTER);

                                                                        table.addCell(c1);

                                                                        table.setHeaderRows(1);

                                                            }else{

                                                                        cellNumber
=checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);

                                                            }                                                         

                                                            cellNumber++;

                                                            break;

                                                case Cell.CELL_TYPE_NUMERIC:

                                                            cellNumber
=checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);

                                                            cellNumber++;

                                                            break;

                                                }                     

                                    }

                                    temp
= 1;

                                    if(numberOfColumns
!= cellNumber){

                                                for(int i=0;i<(numberOfColumns-cellNumber);i++){

                                                            table.addCell(» «);

                                                }

                                    }

                        }

                        subCatPart.add(table);

                        // Now add all this to the document

                        document.add(catPart);

                        document.close();

            }

     Sheet sheet = workbook.getSheetAt (0); — get the first sheet in
the excel file. If you want to read second sheet in the file change the workbook.getSheetAt
(0) to workbook.getSheetAt(1).

// Iterate through each rows from first sheet

Iterator<Row> rowIterator = sheet.iterator();

The above code is used for to iterate the excel sheet row by row.

// For each row, iterate through each columns

Iterator<Cell> cellIterator = row.cellIterator();

The above code is used for to iterate the cells in the row.

Before going to get the cell content it is necessary to
check the cell type to retrieve the content of the cell. I have checked only
for STRING and NUMERICE type there are some of more types available in Cell,
like

Cell.CELL_TYPE_BOOLEAN

Cell.CELL_TYPE_BLANK

Cell.CELL_TYPE_FORMULA

Step 6: Add Meta data in the PDF
document

addMetaData(document)
this method used to add the Meta data in the PDF document.

 private static void addMetaData(Document
document) {

                        document.addTitle(«My first PDF»);

                        document.addSubject(«Using iText»);

                        document.addKeywords(«Java, PDF, iText»);

                        document.addAuthor(«Uvaraj»);

                        document.addCreator(«Uvaraj»);

            }

Step 7: Add Title Page in the PDF
document

addTitlePage(Document document) this method used to add the title
page in the PDF document.

 private static void
addTitlePage(Document document)

                                    throws
DocumentException {

                        Paragraph
preface =
new Paragraph();

                        // We add one empty line

                        addEmptyLine(preface,
1);

                        // Lets write a big header

                        preface.add(new Paragraph(«Title of the document», catFont));

                        addEmptyLine(preface,
1);

                        // Will create: Report generated by: _name, _date

                        preface.add(new Paragraph(«Report generated by: « + «Uvaraj» + «, «

                                                +
new Date(), smallBold));

                        addEmptyLine(preface,
3);

                        preface.add(new Paragraph(

                                                «This document describes something which is very
important «
,

                                                smallBold));

                        addEmptyLine(preface,
8);

                        preface.add(new Paragraph(

                                                «This document is a preliminary version  ;-).»,
redFont));

                        document.add(preface);

                        // Start a new page

                        document.newPage();

            }

Step 8: Add Empty Lines in the PDF document

addEmptyLine(preface,
8) this method used to add empty lines in the PDF document.

 private static void
addEmptyLine(Paragraph paragraph,
int number) {

                        for (int i = 0; i
< number; i++) {

                                    paragraph.add(new Paragraph(» «));

                        }

            }

Step 9: Check empty cell in the
Excel and create empty cell in the PDF document Table.

            I have done
manually for empty cell checking using java code for to create empty cell in
the PDF document table. You can do the excel empty cell checking using

Cell.CELL_TYPE_BLANK.     

            checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table)
used to check whether the excel sheet cell empty if it is then it will create
empty cell in the PDF document table.

private static int
checkEmptyCellAndAddCellContentToPDFTable(
int cellNumber, Cell cell, PdfPTable
table) {

                        if (cellNumber
== cell.getColumnIndex()) {

                                    if(cell.getCellType()
== Cell.
CELL_TYPE_NUMERIC){

                                                table.addCell(Double.toString(cell.getNumericCellValue()));

                                    }

                                    if(cell.getCellType()
== Cell.
CELL_TYPE_STRING){

                                                table.addCell(cell.getStringCellValue());

                                    }

                        } else {

                                    while(
cellNumber < cell.getColumnIndex()) {

                                                            table.addCell(» «);

                                                            cellNumber++;

                                    }

                                    if (cellNumber
== cell.getColumnIndex()) {

                                                if(cell.getCellType()
== Cell.
CELL_TYPE_NUMERIC){

                                                            table.addCell(Double.toString(cell.getNumericCellValue()));

                                                }

                                                if(cell.getCellType()
== Cell.
CELL_TYPE_STRING){

                                                            table.addCell(cell.getStringCellValue());

                                                }

                                    }

                                    cellNumber
= cell.getColumnIndex();

                        }         

                        return
cellNumber;

            }

 CARTOON728X90

Conclusion:

     Once
executed the program with excel sheet, the program will create the PDF file
with table.

 Input:

Output:


Содержание

  1. ThinkTibits!
  2. Input Excel Table Data:
  3. XLS to PDF – JAR Files
  4. Excel to PDF – Java Program Example
  5. Example Output
  6. Share this Post
  7. 14 comments:
  8. Excel to pdf java poi
  9. Java Apache POI Excel save as PDF
  10. 5 Answers 5
  11. Add on to assylias’s answer
  12. In Java:
  13. Convert Excel Files to PDF using Java
  14. Java Excel to PDF Converter — Free Download#
  15. Convert Excel XLS to PDF in Java#
  16. Java Save Excel XLS as PDF — Set PDF Compliance#
  17. Export Excel XLSX to PDF in Java — One Page Per Sheet#
  18. Java Excel to PDF — Export Range of Sheets to PDF#
  19. Conclusion#
  20. Преобразование Excel в PDF с помощью Java
  21. Установка библиотеки Java Excel#
  22. Преобразование Excel в PDF с помощью Java программно#
  23. API преобразования Excel в PDF — дополнительные параметры#
  24. Получить бесплатную лицензию#
  25. Подведение итогов#
  26. Задайте вопрос#

ThinkTibits!

Input Excel Table Data:

XLS to PDF in Java- Input Spreadsheet

XLS to PDF – JAR Files

Excel to PDF – Java Program Example

Example Output

Excel to PDF in Java — Output PDF Document

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?

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

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

how to make the whole content merge exactly into PDF?

@Anonymous, define «exactly» and show an example of your content.

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

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?)

What about if the excel has empty cell.

how make number of columns dynamic

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 Reply Delete

getting error for ‘com.itextpdf.text.Element’

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

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

Источник

Excel to pdf java poi

This is an example article for how to read excel file both (.xlsx and .xls) and convert the PDF document with table of contents. I hope this tutorial will help for the beginners who are interested in to use Apache POI with iText PDF.

Here I have used the Apache POI for reading the excel file because it support to read both .xlsx and .xls files and used iText PDF for to create PDF document.

Table of Content

1. Apache POI Introduction.

1.1 Use of HSSF and XSSF.

2. iText PDF Introduction.

4. Adding Libraries into Build Path.

4.1 Configure Build Path in Eclipse

4.2 Add External Jars in Eclipse

5. Step by Step Explanation about Source Code.

5.1 Import Libraries

5.3 Load Spreadsheet

5.4 Get File Extension

5.5 Read Spreadsheet

5.6 Add Meta data in the PDF document

5.7 Add Title Page in the PDF document

5.8 Check empty cell in the Excel and create empty cell in the PDF document Table.

1. Apache POI Introduction:

Apache POI , a project run by the Apache Software Foundation , and previously a sub-project of the Jakarta Project , provides pure Java libraries for reading and writing files in Microsoft Office formats , such as Word , PowerPoint and Excel .

The Apache POI project contains the following subcomponents:

· POIFS (Poor Obfuscation Implementation File System) – This component reads and writes Microsoft’s OLE2 Compound document. Since all Microsoft Office files are OLE2 files, this component is the basic building block of all the other POI elements. POIFS can therefore be used to read a wider variety of files, beyond those whose explicit decoders are already written in POI.

· HSSF (Horrible Spreadsheet Format) – reads and writes Microsoft Excel (XLS) format files. It can read files written by Excel 97 onwards; this file format is known as the BIFF 8 format. As the Excel file format is complex and contains a number of tricky characteristics, some of the more advanced features cannot be read.

· XSSF (XML Spreadsheet Format) – reads and writes Office Open XML (XLSX) format files. Similar feature set to HSSF, but for Office Open XML files.

· HPSF (Horrible Property Set Format) – reads «Document Summary» information from Microsoft Office files. This is essentially the information that one can see by using the File|Propertiesmenu item within an Office application.

· HWPF (Horrible Word Processor Format) – aims to read and write Microsoft Word 97 (DOC) format files. This component is in initial stages of development.

· HSLF (Horrible Slide Layout Format) – a pure Java implementation for Microsoft Power Point files. This provides the ability to read, create and edit presentations (though some things are easier to do than others)

· HDGF (Horrible Diagram Format) – an initial pure Java implementation for Microsoft Visio binary files. It provides an ability to read the low level contents of the files.

· HPBF (Horrible Publisher Format) – a pure Java implementation for Microsoft Publisher files.

· HSMF (Horrible Stupid Mail Format) – a pure Java implementation for Microsoft Outlook MSG files.

· DDF (Dreadful Drawing Format) – a package for decoding the Microsoft Office Drawing format.

1.1 Use of HSSF and XSSF.

For reading excel we can use any one of HSSF or XSSF, but both are excel version based. In my example I have used both HSSF and XSSF with SS Model (SS = HSSF + XSSF) for to read both .xlsx and .xls file. Here below I have mentioned the use of both XSSF and HSSF model.

HSSF and XSSF provide ways to read spreadsheets create, modify, read and write XLS spreadsheets. They provide:

2. iText PDF Introduction

In my example I have used the iText API for to create the PDF document and make the table based on spreadsheet content. Because iText API provides an direct way to make an document with Paragraphs, Tables and Fonts and easy to understand.

Both Apache POI and iText are Open source, so we don’t want to pay cost for to use those APIs.

1. JDK 1.4 or Later

2. Apache POI- 3.9.

3. iText PDF – 5.1.0

I have attached source code in end of the tutorial. Download those and run it.

4. Adding Libraries into Build Path

Do the below steps in eclipse to add Jars into Project build Path.

Step1: Configure Build Path in Eclipse

Right click the project folder and choose the Build Path -> Configure Build Path. Like below,

Step2: Add External Jars in Eclipse

Click the Libraries tab. Click the Add External Jar button to add Jar once done this. Go to the Order and Export tab and choose Select All then click ok.

6. Step by Step Explanation about Source Code

Step 1: Import Libraries

Import the Libraries, if you are using eclipse IDE then it will automatically imported when you are using those.

Источник

Java Apache POI Excel save as PDF

How can I convert/save excel file to pdf ? I’m using java play framework to generate some excel files and now the requirement changes to pdf . I don’t want to recode everything.

Is there a way to convert to pdf ?

The excel files I’m generating are from a template; I read the excel template file, write changes, and save as new excel file. That way, the template is unchanged. It contains border, image, and other formatting.

5 Answers 5

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

Try this Example to convert XLS to PDF

The complete Java code that accepts Excel spreadsheet data as an input and transforms that to a PDF table data is provided below:

i hope this will help you

Add on to assylias’s answer

The code from assylias above was very helpful to me in solving this problem. The answer from santhosh could be great if you don’t care about the resulting PDF looking exactly like your excel pdf export would look. However, if you are, say, filling out an excel template using Apache POI an then trying to export that while preserving its look and not writing a ton of code in iText just to try to get close to that look, then the VBS option is quite nice.

I’ll share a Java version of the kotlin assylias has above in case that helps anyone. All credit to assylias for the general form of the solution.

In Java:

An alternative is to use a VB script and call it from Java.

Источник

Convert Excel Files to PDF using Java

Excel to PDF conversion might be required in various scenarios for exporting tabular data from worksheets to PDF pages. In this article, you’ll learn how to convert Excel files to PDF using Java in order to automate XLS/XLSX to PDF conversion within your web or desktop applications.

Java Excel to PDF Converter — Free Download#

Aspose.Cells for Java is a well-known spreadsheet processing API that lets you create, manipulate, and convert Excel XLS/XLSX and other spreadsheet formats quite easily. With a few lines of code and easy to use methods, you can perform quality Excel to PDF conversion with high fidelity. Aspose.Cells for Java can be downloaded as JAR or installed using the following Maven configurations.

Repository:

Dependency:

Convert Excel XLS to PDF in Java#

The following are the steps to convert an Excel XLS or XLSX file to PDF in Java. The links to API references let you explore more about the classes and methods of the API.

  • Create an object of Workbook class and initialize it with the path to Excel file.
  • Save the Excel file as PDF using Workbook.save(String, SaveFormat) method.

The following code sample shows how to convert an XLSX to PDF in Java.

Java Save Excel XLS as PDF — Set PDF Compliance#

PDF format supports various compliance standards such as PDF/A and etc. Being compliant with a particular standard means that the file fulfills the requirements or rules defined in that standard. In order to convert Excel to PDF with a particular compliance standard, you can use the PdfSaveOptions class.

The following are the steps to set a particular compliance standard in Excel to PDF conversion.

  • Initialize Workbook object with the Excel file’s path.
  • Create an instance of the PdfSaveOptions class.
  • Set the compliance using PdfSaveoptions.setCompliance(PdfCompliance) method.
  • Save the Excel file as PDF using Workbook.save(String, PdfSaveOptions) method.

The following code sample shows how to convert XLSX to PDF with a particular PDF standard using Java.

Export Excel XLSX to PDF in Java — One Page Per Sheet#

By default, the API renders the worksheets according to the page size in the PDF document. In this case, one worksheet can possibly be rendered on multiple pages in the PDF. In order to override this operation, you can configure the API to render all the content of a worksheet on one page using PdfSaveOptions.setOnePagePerSheet(boolean) method.

The following code sample shows how to convert Excel XLSX to PDF with one page per sheet settings in Java.

Java Excel to PDF — Export Range of Sheets to PDF#

In certain cases, you may need to convert only a selective range of the Excel sheets instead of the whole workbook. In such a case, you can tell the API about the range of the sheets to be included in the rendering process using PdfSaveOptions.setPageIndex(int) and PdfSaveOptions.setPageCount(int) methods.

The following code sample shows how to render a range of sheets in Excel XLS to PDF using Java.

Conclusion#

In this article, you have learned how to convert Excel files to PDF using Java. Furthermore, you have seen how to customize Excel to PDF conversion in various scenarios.

Источник

Преобразование Excel в PDF с помощью Java

Преобразование Excel в PDF с помощью Java

Загрузите исходный файл Excel, обработайте его и преобразуйте в формат файла PDF, используя несколько строк исходного кода. Эта Java библиотека предоставляет огромный набор методов для быстрого и эффективного преобразования и обработки файлов. Недавно мы опубликовали статью, в которой показано, как преобразовать Excel в PDF в приложении Node.js. В этом сообщении блога мы узнаем, как программно преобразовать Excel в PDF с помощью Java.

Будут затронуты следующие моменты:

Установка библиотеки Java Excel#

Мы собираемся использовать Java Excel API, который предоставляет исчерпывающую документацию по использованию. Однако его легко интегрировать с Java-приложениями.

Вы можете либо скачать файлы JAR, либо следовать следующим конфигурациям Maven:

Преобразование Excel в PDF с помощью Java программно#

В этом разделе мы узнаем, как программно преобразовать Excel в PDF. Он позволяет выполнять различные действия, отслеживать процесс преобразования огромных файлов и многое другое.

Мы рассмотрим следующие шаги и фрагмент кода:

  1. Создайте объект Workbook и инициализируйте его исходным файлом Excel.
  2. Получите доступ к первому рабочему листу с помощью метода getWorksheets().get(0).
  3. Создайте экземпляр класса PdfSaveOptions, чтобы получить доступ к параметрам сохранения файла PDF.
  4. Вызовите метод setCompliance, чтобы установить соответствие PDFA-1a.
  5. Получите доступ к ячейке B8, вызвав метод getCells().
  6. Вызовите метод putValue, чтобы вставить какое-либо значение в ячейку B8.
  7. Найдите и замените строку, вызвав метод replace.
  8. Сохраните выходной файл PDF, вызвав метод save.

На следующем изображении показан вывод приведенного выше фрагмента кода:

API преобразования Excel в PDF — дополнительные параметры#

В предыдущем разделе мы рассмотрели фрагмент кода для программного преобразования файла MS Excel в файл PDF. Мы также реализовали некоторые другие методы, такие как поиск и замена текста и добавление текста в файл. Однако вы можете изучить документацию этого API Java Excel, чтобы узнать о других функциях.

Получить бесплатную лицензию#

Вы можете воспользоваться бесплатной временной лицензией, чтобы попробовать API без ограничений на пробную версию.

Подведение итогов#

Мы заканчиваем этот пост в блоге здесь. Мы надеемся, что вы научились программно конвертировать Excel в PDF с помощью Java. Кроме того, эта библиотека позволяет вам делать больше, чем преобразование файлов. Тем не менее, эта статья поможет вам, если вы хотите использовать API преобразования Excel в PDF для своего Java-приложения. Есть и другие соответствующие статьи, упомянутые в разделе «См. также» ниже. Наконец, conholdate.com находится в постоянном процессе написания новых сообщений в блоге. Поэтому оставайтесь на связи, чтобы быть в курсе последних обновлений.

Задайте вопрос#

Вы можете сообщить нам о своих вопросах или запросах на нашем форуме.

Источник

Понравилась статья? Поделить с друзьями:
  • Apache poi date excel
  • Apa style word document
  • Apa style reference in word
  • Apa style for word
  • Apa citations in word