1

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 but directly below the last line of text. The amount of text may vary so I need to calculate if there is enough room at the bottom of the page to insert my template.

Thanks for any help, Aaron

Aaron
  • 433
  • 11
  • 15
  • To determine where the text on the given page ends, have a look at the iText in Action, 2nd edition example [ShowTextMargins](http://itextpdf.com/examples/iia.php?id=280) which parses a PDF and adds a rectangle showing the text margin. – mkl Jan 15 '14 at 20:19
  • How do I show this question has been answered in the comments? – Aaron Jan 15 '14 at 21:27
  • I created an actual answer with the contents of my comment. You now can accept that answer as usual. – mkl Jan 16 '14 at 08:06

1 Answers1

1

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.

Community
  • 1
  • 1
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Is there a way to do this if you have a footer on the page? i.e. I want to insert some text between the last bit of text and the footer, but TextMarginFinder just finds the footer. – Rowan Feb 01 '16 at 23:51
  • 1
    @Rowan *Is there a way to do this if you have a footer on the page?* - I have two options on my mind for that: **1** If you *know* about your documents that the footer (if a document has any) always is below a certain **y** coordinate and content always is above it, then you can filter the `TextMarginFinder` input to only take content above that line into account. Alternatively, **2** you can use the `FreeSpaceFinder[Ext]` from [this answer](http://stackoverflow.com/a/26503289/1729265) to find sufficiently large patches of empty space on a page, in your case it should be as wide as the page. – mkl Feb 02 '16 at 07:21