itext7 pdf to image

20,656

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);
Share:
20,656
jkb016
Author by

jkb016

Wrote my first code 14 years ago and since then love doing anything new and out of the box.

Updated on July 10, 2022

Comments

  • jkb016
    jkb016 almost 2 years

    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();
    
  • jkb016
    jkb016 about 8 years
    Thanks a lot for all the info .
  • scrat.squirrel
    scrat.squirrel almost 6 years
    Can somebody specify in which namespace does this "Image" class above exists?
  • Bruno Lowagie
    Bruno Lowagie almost 6 years
    @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.
  • Christlin Panneer
    Christlin Panneer about 5 years
    How to set this image to picturebox?
  • Liran Friedman
    Liran Friedman about 5 years
    @BrunoLowagie, Can you please post a sample of how to save the converted pdf as a png image file?
  • V. Z
    V. Z almost 3 years
    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/maste‌​r/…