- Java Pdf To Word
- How To Convert Excel To Pdf In Office 2010
- How To Convert Excel To Pdf In Office 2007
- Change Pdf File To Excel Format
- How To Convert Excel To Pdf
- Java Convert Excel To Pdf Free
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.
- Q: Does jOfficeConvert support converting.docx documents to PDF? A: Yes, jOfficeConvert, Qoppa’s java library to convert Word documents to PDF supports the latest Microsoft Word 2007-2010 format (Word files with a.docx extension) in addition to supporting Word 97-2003 (Word files with a.doc extension).
- Sep 20, 2017 Write each Excel worksheet to a separate PDF; Converting an Excel Worksheet to PDF; Command Line Word to PDF Conversion on Windows or Linux; Convert.dotx files to PDF in Java using jOfficeConvert; Excel to PDF Conversion Options; Convert an Excel worksheet to a Tiff Image in Java using jOfficeConvert.
Active1 year, 6 months ago
How to convert PDF file to Excel using Java.We have generate PDF file using itext. Now we want to convert it to excel.we want sample code to convert pdf to excel.or kindly Suggest API.
T.SamyT.Samy
closed as off-topic by Joe C, Andrew Thompson, K.Dᴀᴠɪs, klutt, Saddam Abu GhaidaMar 16 '18 at 9:53
This question appears to be off-topic. The users who voted to close gave this specific reason:
- 'Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.' – Joe C, Andrew Thompson, K.Dᴀᴠɪs, klutt
1 Answer
You can convert a PDF document to an Excel workbook with Aspose.PDF API by using below code snippet. Please ensure using Aspose.PDF for Java 18.2 in your environment.
In case you notice any problem with generated file, please share it with us by uploading it to any file sharing server like Google Drive, Dropbox etc. Also share your environment details (OS details, JDK/JRE version etc) so that we can try to reproduce and investigate the issue to help you out.
I work with Aspose as Developer Evangelist.
Farhan RazaFarhan Raza
Not the answer you're looking for? Browse other questions tagged javaexcelapacheitextaspose or ask your own question.
Hi,This is an example article for how to read excelfile 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 useApache POI with iText PDF.Here I have used the Apache POI for reading theexcel file because it support to read both .xlsx and .xls files and used iTextPDF 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.1Configure Build Path in Eclipse
4.2Add 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.ApachePOI Introduction:
Apache POI, a project run by theApache Software Foundation, and previously a sub-project of theJakarta Project, provides pureJavalibraries for reading and writing files inMicrosoft Officeformats, such asWord,PowerPointandExcel.
The Apache POI project contains thefollowing subcomponents:
·POIFS (Poor Obfuscation Implementation File System) – Thiscomponent reads and writes Microsoft's OLE2 Compound document. Sinceall Microsoft Office files are OLE2 files, this component is thebasic building block of all the other POI elements. POIFS can therefore be usedto read a wider variety of files, beyond those whose explicit decoders arealready written in POI.
·HSSF (Horrible Spreadsheet Format) – reads andwrites Microsoft Excel (XLS) format files. It can read files writtenby Excel 97 onwards; this file format is known as the BIFF8 format. As the Excel file format is complex and contains a number oftricky characteristics, some of the more advanced features cannot be read.
·XSSF (XML Spreadsheet Format) – reads and writes Office OpenXML (XLSX) format files. Similar feature set to HSSF, but for Office OpenXML files.
·HPSF (Horrible Property Set Format) – reads 'DocumentSummary' information from Microsoft Office files. This isessentially the information that one can see by using the File|Propertiesmenuitem within an Office application.
·HWPF (Horrible Word Processor Format) – aims to read andwrite Microsoft Word 97 (DOC) format files. This component is ininitial stages of development.
·HSLF (Horrible Slide Layout Format) – a pure Java implementationfor 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 Javaimplementation for Microsoft Visio binary files. It provides anability to read the low level contents of the files.
·HPBF (Horrible Publisher Format) – a pure Java implementation forMicrosoft Publisher files.
·HSMF (Horrible Stupid Mail Format) – a pure Java implementationfor Microsoft Outlook MSG files.
·DDF (Dreadful Drawing Format) – a package for decoding theMicrosoft Office Drawing format.
1.1 Use of HSSF and XSSF.
For reading excel we can use any one of HSSF orXSSF, 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 useof 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. iTextPDF Introduction
In my example I have usedthe iText API for to create the PDF document and make the table based on spreadsheetcontent. Because iText API provides an direct way to make an document with Paragraphs,Tables and Fonts and easy to understand.
Both Apache POI and iTextare Open source, so we don’t want to pay cost for to use those APIs.
3.Requirements:
1.JDK 1.4 orLater
2.ApachePOI- 3.9.
3.iText PDF– 5.1.0
4.dom4j-1.6.1
5.xmlbeans-2.3.0
I have attached source codein end of the tutorial. Download those and run it.
4. Adding Libraries into Build Path
Do the below steps ineclipse to add Jars into Project build Path.
Step1: Configure Build Path in Eclipse
Right click the project folder and choose the Build Path -> ConfigureBuild Path. Like below,
Step2: Add External Jars in Eclipse
Clickthe 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
Importthe Libraries, if you are using eclipse IDE then it will automatically importedwhen you are using those.
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
import java.io.IOException;
importjava.io.InputStream;
importjava.util.Date;
importjava.util.Iterator;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.openxml4j.opc.OPCPackage;
importorg.apache.poi.ss.usermodel.Cell;
importorg.apache.poi.ss.usermodel.Row;
importorg.apache.poi.ss.usermodel.Sheet;
importorg.apache.poi.ss.usermodel.Workbook;
importorg.apache.poi.xssf.usermodel.XSSFWorkbook;
importcom.itextpdf.text.Anchor;
importcom.itextpdf.text.BaseColor;
importcom.itextpdf.text.Chapter;
importcom.itextpdf.text.Document;
importcom.itextpdf.text.DocumentException;
importcom.itextpdf.text.Element;
importcom.itextpdf.text.Font;
importcom.itextpdf.text.Paragraph;
importcom.itextpdf.text.Phrase;
importcom.itextpdf.text.Section;
![Java Java](https://www.filebuzz.com/software_screenshot/full/office_convert_excel_to_pdf_free-194157.jpg)
importcom.itextpdf.text.pdf.PdfPCell;
importcom.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
The above libraries I have used in the example application for toread excel and make PDF.
Step 2: Entry Point
Main method is the entry point of this example. Here Ihave hardcoded the inputs excel file. Change the path to refer your excel file.
publicstaticvoid main(String[] args) {
StringexcelFile = 'E:Uvarajnewarticleexample.xlsx';
FilexlsFile = new File(excelFile);
![Java Convert Excel To Pdf Java Convert Excel To Pdf](https://i.ytimg.com/vi/XEBr12uxG2I/maxresdefault.jpg)
Workbookworkbook;
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 theworkbook. See the section 1.1 Use of HSSF and XSSF for details.
privatestaticWorkbook loadSpreadSheet(File xlsFile) throws Exception {
Workbookworkbook = null;
Stringext = getFileExtension(xlsFile.getName());
if(ext.equalsIgnoreCase('xlsx')) {
OPCPackagepkg = OPCPackage.open(xlsFile.getAbsolutePath());
workbook= new XSSFWorkbook(pkg);
pkg.close();
} elseif(ext.equalsIgnoreCase('xls')) {
InputStreamxlsFIS = new FileInputStream(xlsFile);
workbook= new HSSFWorkbook(xlsFIS);
xlsFIS.close();
} else {
thrownew Exception('FILE EXTENSION NOT RECOGNIZED');
}
returnworkbook;
}
Step 4: Get File Extension
getExtension (xlsFile.getName ()) is used for to get theextension of the file.
privatestaticString getFileExtension(String fileName) {
Stringext = ';
int mid = fileName.lastIndexOf('.');
ext =fileName.substring(mid + 1, fileName.length());
System.out.println('File Extension --' + ext);
returnext;
}
Step 5:Read Spreadsheet
readSpreadSheet(workbook)used to read the excel file and create the PDF document with table.
privatestaticvoid readSpreadSheet(Workbook workbook) throwsIOException,
DocumentException{
Documentdocument = new Document();
PdfWriter.getInstance(document,new FileOutputStream(FILE));
document.open();
addMetaData(document);
addTitlePage(document);
Anchoranchor = new Anchor('FirstChapter', catFont);
anchor.setName('First Chapter');
// Second parameter is the number of the chapter
ChaptercatPart = new Chapter(new Paragraph(anchor), 1);
ParagraphsubPara = new Paragraph('Table', subFont);
SectionsubCatPart = catPart.addSection(subPara);
addEmptyLine(subPara,5);
Sheetsheet = workbook.getSheetAt(0);
// Iterate through each rows from first sheet
Iterator<Row>rowIterator = sheet.iterator();
int temp = 0;
booleanflag = true;
PdfPTabletable = null;
while(rowIterator.hasNext()) {
Rowrow = 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()) {
Cellcell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
if (temp 0){
numberOfColumns= row.getLastCellNum();
PdfPCellc1 = 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 inthe 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 tocheck the cell type to retrieve the content of the cell. I have checked onlyfor 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 PDFdocument
addMetaData(document)this method used to add the Meta data in the PDF document.
privatestaticvoid addMetaData(Documentdocument) {
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 PDFdocument
addTitlePage(Document document) this method used to add the titlepage in the PDF document.
privatestaticvoidaddTitlePage(Document document)
throwsDocumentException {
Paragraphpreface = 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 veryimportant ',
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.
privatestaticvoidaddEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i< number; i++) {
paragraph.add(new Paragraph(' '));
}
}
Step 9: Check empty cell in theExcel and create empty cell in the PDF document Table.
I have donemanually for empty cell checking using java code for to create empty cell inthe 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 createempty cell in the PDF document table.
privatestaticintcheckEmptyCellAndAddCellContentToPDFTable(int cellNumber, Cell cell, PdfPTabletable) {
if (cellNumber cell.getColumnIndex()) {
if(cell.getCellType() Cell.CELL_TYPE_NUMERIC){
Java Pdf To Word
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){
How To Convert Excel To Pdf In Office 2010
table.addCell(cell.getStringCellValue());
}
}
How To Convert Excel To Pdf In Office 2007
cellNumber= cell.getColumnIndex();
Change Pdf File To Excel Format
}
returncellNumber;
}
Conclusion:
Onceexecuted the program with excel sheet, the program will create the PDF filewith table.
Input:
How To Convert Excel To Pdf
Output: