Find location last object on PDF page using iTextSharp

前端 未结 1 949
盖世英雄少女心
盖世英雄少女心 2020-12-21 21:14

How can I find the location and the height of the last object on a PDF page? There is no footer. I\'m wanting to stamp a template below the last item. Not in the footer b

相关标签:
1条回答
  • 2020-12-21 21:48

    To determine where text and (bitmap) images on a given page end, have a look at the iText in Action, 2nd edition example ShowTextMargins which parses a PDF and adds a rectangle showing the text and (bitmap) images margin.

        PdfReader reader = new PdfReader(src);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        TextMarginFinder finder;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            finder = parser.processContent(i, new TextMarginFinder());
            PdfContentByte cb = stamper.getOverContent(i);
            cb.rectangle(finder.getLlx(), finder.getLly(),
                finder.getWidth(), finder.getHeight());
            cb.stroke();
        }
        stamper.close();
        reader.close();
    

    Be aware, though, that this mechanism ignores vector graphics (often also used for underlining text, colored backgrounds, and decorative separating lines) because the underlying iText parser package still (as of iText version 5.4.5) ignores them.

    Update: As of version 5.5.6 iText extends the parser package to also include vector graphics parsing. This allows for an extension of the original TextMarginFinder class implementing the new ExtRenderListener methods as presented in this answer resulting in an analogous class MarginFinder which does not only consider text but also other kind of content, e.g. bitmap images and vector graphics.

    0 讨论(0)
提交回复
热议问题