Itext excel to pdf

iText pdfOffice is a Java add-on for the iText 7 Core PDF library which enables high-quality native conversion of Microsoft Office documents to PDF.

Currently supporting Word, PowerPoint and Excel formats, it provides an API which allows developers to programmatically convert documents and easily integrate with existing iText workflows.

Don’t use Java?

Reach out to us anyway and tell us about your requirements. We’ll see what we can do for you!


Contact us

How pdfOffice works

pdfOffice was developed to provide best-in-class conversion of Office documents, without the need for costly licensing subscriptions and conversion costs. It can handle files created in Office 97 through Office 2019 and the latest Office 365 updates.  

Unlike certain competing solutions it does not rely on any external software to perform conversion, everything is handled natively. As such, all you need is the pdfOffice add-on and a valid license to enable high-fidelity conversion of all supported document formats. 

As you can see in the following code example, with pdfOffice you get great results in no time. This code sample demonstrates a simple conversion of a Word document (.docx) into a PDF. 

import com.itextpdf.licensekey.LicenseKey;
import com.itextpdf.pdfoffice.OfficeConverter;
import com.itextpdf.pdfoffice.OfficeDocumentConverterProperties;
import com.itextpdf.pdfoffice.OfficePageRange;

import java.io.File;
import java.io.IOException;

public class Office {

public static void main(String[] args) throws IOException {

LicenseKey.loadLicenseFile(«license.xml»);

File pdfOutFile = new File(«out_office.pdf»);
File demoFile = new File(«input.docx»);
// converts pages between 1 to 8
OfficeConverter.convertOfficeDocumentToPdf(demoFile, pdfOutFile, new OfficeDocumentConverterProperties()
.setPageRange(new OfficePageRange().addPageSequence(1, 8)));
}
}

pdfOffice is not available yet for C#

Benefits

Why use pdfOffice?

Over 500 billion Office documents are created every year across multiple industries, yet they have significant drawbacks when it comes to use cases such as distribution, information governance data security policies, or archiving. With pdfOffice you get highly-accurate PDF versions of the same documents which can be widely-distributed, manipulated, or processed to allow advanced PDF features such as digital signatures, secure redaction, annotations, and much more.

Modern web browsers have wide support for viewing PDFs and using pdfOffice in combination with pdfRender means accurate representations of Office documents can be viewed in any device or application, with no additional requirements.
 

High-fidelity native document conversion

The output from pdfOffice closely matches source documents. Text and paragraph formatting is preserved, line and page breaks are retained, and images, shapes, tables, WordArt and SmartArt in documents are supported. In addition, no third-party software is required for conversion.

Automate document conversion and processing

pdfOffice’s API gives developers the ability to automate the conversion of documents, and process input and output streams in addition to files.

Take advantage of iText’s advanced PDF capabilities

Secure documents with digital signatures, password encryption, and redact sensitive information. Use Document Assembly to combine data from multiple documents, add annotations and file attachments, create image renders of documents, etc.

KEY FEATURES

Core capabilities of pdfOffice

pdfOffice is ideal for automation and batch operations, enabling high-fidelity conversion of documents whether you just need accurate PDF versions of your documents or want to integrate its capabilities into your existing iText workflows.

pdfOffice was specifically designed to convert Office documents to PDF, while preserving all the formatting present in such files. As such, it does not support other file types. For example, if you’ve saved a .txt file as a .docx, or simply renamed a file to change its file extension, you shouldn’t expect pdfOffice to know what to do with it. If you want to make PDFs from .txt or .csv files, iText 7 Core can handle that. Similarly, for .xml or .html, you can use pdfHTML

Best-in-class native document conversion

As an iText product, you can rely on getting accurate conversion and rendering of your documents as ISO-compliant PDFs. And since pdfOffice performs its document conversion natively, it does not require any additional software.

Supports a wide range of document formats

pdfOffice currently supports files in .doc, .docx, .dotx, docm, .dotm, .dot, .ppt, .pptx, .potx, .pptm, .potm, .ppsx, .ppsm, .pot, .pps, xlsx, xls, xlsm, xltx, xltm, xlt, xlam, and xla formats.

Use in combination with other iText add-ons

pdfOffice’s capabilities can be combined with other iText 7 Suite add-ons, such as pdfRender to generate accurate image renders of documents, compress documents with pdfOptimizer, securely redact information with pdfSweep etc.

Wide language and font support

pdfOffice has built-in emulation of over 700 fonts across 45 languages, ensuring ensure great text rendering, worldwide compatibility and avoids font licensing costs. This solution is memory-efficient and ideal for use in embedded environments.

Resources

Here you can find the necessary resources to install and use pdfOffice.

I have an Excel file that has 5 columns having few merged cells, blank cells, dates, and other text information (a normal excel file).

I am reading this file using POI API in java. I am able to convert the file to pdf table using iText jar.

But, the whole format is not copied into the pdf. (e.g., merged cells come into one column, and other formatting or settings are all gone).

A simple pdf table is created.

How do i retain the same format as in excel? (I want exact copy of excel sheet in pdf)

Here is the code that I am using

     //First we read the Excel file in binary format into FileInputStream
             FileInputStream input_document = new FileInputStream(new File("K:\DCIN_TER\DCIN_EPU2\CIRCUIT FROM BRANCH\RAINBOW ORDERS\" + SONo.trim() + "\" + SONo.trim() + " - Checklist.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
             com.itextpdf.text.Document iText_xls_2_pdf = new com.itextpdf.text.Document();

             PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("K:\DCIN_TER\DCIN_EPU2\CIRCUIT FROM BRANCH\RAINBOW ORDERS\" + SONo.trim() + "\" + SONo.trim() + " - Checklist.pdf"));

             iText_xls_2_pdf.open();

             //we have 5 columns in the Excel sheet, so we create a PDF table with 5 columns; Note: There are ways to make this dynamic in nature, if you want to.
             PdfPTable my_table = new PdfPTable(5);

             //We will use the object below to dynamically add new data to the table
             PdfPCell table_cell;

             //Loop through rows.
             while(rowIterator.hasNext())
                    {
                     Row rowi = rowIterator.next();

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

                            while(cellIterator.hasNext())
                            {
                                    Cell celli = cellIterator.next(); //Fetch CELL

                                    switch(celli.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(celli.getStringCellValue()));

                                            //move the code below to suit to your needs
                                            my_table.addCell(table_cell);

                                            break;

                                            case Cell.CELL_TYPE_NUMERIC:

                                            //Push the data from Excel to PDF Cell
                                            table_cell = new PdfPCell(new Phrase("" + celli.getNumericCellValue()));

                                            //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  

I have attached the excel file as an image

excel file as .png file. As you can see, the file is a simple one. I want the same styles as in Excel in pdf also. Please guide me

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

1
branch

0
tags


Code

  • Use Git or checkout with SVN using the web URL.

  • Open with GitHub Desktop

  • Download ZIP

Latest commit

Files

Permalink

Failed to load latest commit information.

Type

Name

Latest commit message

Commit time

excel-to-pdf

An easy way to convert Excel 2003 and Excel 2007 to PDF by Java code based on Apache POI and itext7

使用SDK

IExcel2PDF excel2PdfTool = EPFactory.getEP("case6.xlsx", "output1.pdf", System.getProperty("user.dir") + "/doc/font/SimHei.TTF");
if(excel2PdfTool != null) {
    excel2PdfTool.convert();
}

maven

Maven 引用方式

<dependency>
    <groupId>com.github.zhangchunsheng</groupId>
    <artifactId>excel-to-pdf</artifactId>
    <version>1.0.2</version>
</dependency>
https://github.com/itext/itext7
https://www.tutorialspoint.com/itext/itext_text_annotation.htm

捐助 donate

Содержание

  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 находится в постоянном процессе написания новых сообщений в блоге. Поэтому оставайтесь на связи, чтобы быть в курсе последних обновлений.

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

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

Источник

Понравилась статья? Поделить с друзьями:
  • Italics not working in word
  • Italics in word processing
  • Italicized word in a sentence
  • Italicize text in word
  • Italian word for you understand