So in our Signature service we had a portion where we have to determine the documents content ending position so that we can put the stamper below the content of the whole document (Basically from the last page). Since we were using iText 5 we have this code to achieve this:
PdfReaderContentParser parser = new PdfReaderContentParser(reader); TextMarginFinder finder; int lastPage = reader.getNumberOfPages(); finder = parser.processContent(lastPage, new TextMarginFinder()); appearance.setVisibleSignature(new Rectangle(finder.getWidth() - 100, finder.getLly() - 200,finder.getWidth() + 150, finder.getLly()), lastPage, fieldName);
Now we are migrating from iText 5 to iText 8, so it seems the above approach no longer works. After searching through many post here and documentation I found very close to solution which is what I tried using the below:
TextMarginFinder strategy = new TextMarginFinder(); PdfCanvasProcessor parser = new PdfCanvasProcessor(strategy); parser.processPageContent(pdfDoc.getPage(pdfDoc.getNumberOfPages())); Rectangle textRectangle = strategy.getTextRectangle(); float left = textRectangle.getBottom(); float right = textRectangle.getRight(); float width = textRectangle.getWidth(); PdfSignatureAppearance appearance = signer.getSignatureAppearance(); appearance .setLayer2FontSize(6) .setPageRect(new Rectangle(width - 50, textRectangle.getBottom(), 100, 50)) .setPageNumber(pdfDoc.getNumberOfPages()) .setCertificate(chain[0]) .setSignatureCreator("ORIGEM"); signer.setFieldName(fieldname); pdfDoc.close();
But this is not giving me correct result at all. I then tried the below solutions taking hints from @mkl's answer from here:
PdfWriter pdfWriter = new PdfWriter(src); PdfDocument pdfDocument = new PdfDocument(pdfWriter); Document document = new Document(pdfDocument); { for (int i = 0; i < 30; i++) { Rectangle currentBox = document.getRenderer().getCurrentArea().getBBox(); } }
It seems to be closer to what I needed, but I cannot find a way how to get the area for each page, without adding anything using the document object.