How to position text relative to page?

My code gives me inconsistent positions of text.

31st May 2016
admin-marketing

 

int n = reader.getNumberOfPages();
PdfImportedPage page;
PdfCopy.PageStamp stamp;
for (int j = 0; j  n; ) {
    ++j;
    page = writer.getImportedPage(reader, j);
    stamp = writer.createPageStamp(page);
    Rectangle crop = reader.getCropBox(1);
    // add overlay text
    Phrase phrase = new Phrase("Overlay Text");
    ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER,
        phrase, crop.getRight(72f), crop.getHeight() / 2, 90);
    stamp.alterContents();
    writer.addPage(page);
}
The code above gives me inconsistent positions of text. In some pages, only a portion of the "Overlay text" is visible. Please help, I don't know how to properly use mediabox and cropbox and I'm new to iText.

 

Posted on StackOverflow on Jul 13, 2013 by euler

Regarding the inconsistent position: that should be fixed by adding the vertical offset:

crop.getRight(72f), crop.getBottom() + crop.getHeight() / 2

Do you see? You took the right border with a margin of 1 inch as x coordinate, but you forgot to take into account the y coordinate of the bottom of the page (it's not always 0). Normally, this should fix the positioning problem.

Click this link if you want to see how to answer this question in iText 5.


Share this article

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now