itext7 pdf to image

前端 未结 1 993
忘掉有多难
忘掉有多难 2020-12-15 12:34

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         


        
相关标签:
1条回答
  • 2020-12-15 13:20

    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);
    
    0 讨论(0)
提交回复
热议问题