Skip to main content
Skip table of contents

How to position text relative to page?

My code gives me inconsistent positions of text.

How do I set the position of text so that it is centered vertically relative to its page size? I want to position it say for example x number of points from right and centered vertically. The text of course is rotated 90 degrees.

 

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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.