itext7 pdf to image
Asked Answered
B

1

11

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();
Basidiospore answered 14/6, 2016 at 10:8 Comment(0)
K
22

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);
Kangaroo answered 14/6, 2016 at 13:2 Comment(6)
Thanks a lot for all the info .Basidiospore
Can somebody specify in which namespace does this "Image" class above exists?Cheerless
@woohoo In Java: com.itextpdf.layout.element.Image; In C#: iText.Layout.Element.Image. Please consult the Java API docs or the C# tutorial where all of these basic questions are answered. Important: I am assuming that you are using iText 7.Kangaroo
How to set this image to picturebox?Wadsworth
@BrunoLowagie, Can you please post a sample of how to save the converted pdf as a png image file?Madagascar
It can be late but, they have PdfRender for Java now pdfa.org/…. Soon there will be for C#, but use Apitron instead to do that job in C#. Here is an example: github.com/apitron/Apitron.PDF.Rasterizer.Samples/blob/master/…Marcellemarcellina

© 2022 - 2024 — McMap. All rights reserved.