Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itext7 pdf to image

Tags:

itext

itextpdf

I am using iText7(java) and am looking for a way to convert a pdf page to image. In older iText versions you could do this :

PdfImportedPage page = writer.getImportedPage(reader, 1);
Image image = Image.getInstance(page);

But iText7 does not have PdfImportedPage .

My use case, I have a one page pdf file. I need to add a table and resize the pdf contents to fit a single page. In old iText I would create a page , add table, convert existing pdf page to image, resize image and add that resized image to new page. Is there a new way to do this in iText7.

Thanks to Bruno's answer I got this working with following code :

PdfPage origPage = readerDoc.getPage(1);
Rectangle rect = origPage.getPageSize();
Document document = new Document(writerDoc);
Table wrapperTable = new Table(1);
Table containerTable = new Table(new float[]{0.5f,0.5f});
containerTable.setWidthPercent(100);
containerTable.addCell( "col1");
containerTable.addCell("col2");

PdfFormXObject pageCopy = origPage.copyAsFormXObject(writerDoc);
Image image = new Image(pageCopy);
image.setBorder(Border.NO_BORDER);
image.setAutoScale(true);
image.setHeight(rect.getHeight()-250);
wrapperTable.addCell(new Cell().add(containerTable).setBorder(Border.NO_BORDER));
wrapperTable.addCell(new Cell().add(image).setBorder(Border.NO_BORDER));
document.add(wrapperTable);
document.close();
readerDoc.close();
like image 867
jkb016 Avatar asked Jun 14 '16 10:06

jkb016


People also ask

Is iText PDF free?

We have a dual licensing business model. You can use the SDK at no cost if you open source the application you are integrating the iText SDK into per the terms of the AGPL license, or you can license iText commercially if you don't want to open source your entire application.

Is iText PDF open source?

iText is rooted in the Open-Source community Over 20 years ago, the code that formed the basis for the iText PDF library as we know it today was written in an open-source environment. Even today, its code remains open-source.

What is iText format?

iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. The following tutorial will show how to create PDF files with iText. This tutorial assumes that you have basis Java and Eclipse knowledge. iText has a hierarchical structure.


1 Answers

Please read the official documentation for iText 7, more specifically Chapter 6: Reusing existing PDF documents

In PDF, there's the concept of Form XObjects. A Form XObject is a piece of PDF content that is stored outside the content stream of a page, hence XObject which stands for eXternal Object. The use of the word Form in Form XObject could be confusing, because people might be thinking of a form as in a fillable form with fields. To avoid that confusing, we introduced the term PdfTemplate in iText 5.

The class PdfImportedPage you refer to was a subclass of PdfTemplate: it was a piece of PDF syntax that could be reused in another page. Over the years, we noticed that people also got confused by the word PdfTemplate.

In iText 7, we returned to the basics. When talking about a Form XObject, we use the class PdfFormXObject. When talking about a page in a PDF file, we use the class PdfPage.

This is how we get a PdfPage from an existing document:

PdfDocument origPdf = new PdfDocument(new PdfReader(src));
PdfPage origPage = origPdf.getPage(1);

This is how we use that page in a new document:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf);

If you want to use that pageCopy as an Image, just create it like this:

Image image = new Image(pageCopy);
like image 133
Bruno Lowagie Avatar answered Nov 02 '22 12:11

Bruno Lowagie