How to place text relative to a page using iText?

How to set the position of the text so that it is centered vertically relative to its page size? I want to place it, for example, x is the number of points on the right and in the center 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 above code gives me an inconsistent text position, and on some pages only the β€œText overlay” part is visible. Please help, I do not know how to use mediabox and cropbox correctly, and I am new to itext.

Thanks!

+2
java pdf itext
source share
1 answer

Regarding inconsistent position: this should be fixed by adding vertical offset:

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

You see? You took the right border with a 1 inch edge as the x coordinate, but you forgot to consider the y coordinate of the bottom of the page (this is not always 0 ). This should usually fix the positioning problem.

Regarding the fact that only part of the overlay text is visible: at first I assumed that you were adding content to existing content, but this assumption is wrong (you are using getOverContent() ). What exactly do you mean by this second question? Do you know that text is trimmed by CropBox? Are you looking for a way to measure the contents of a phrase to see if it matches the height before adding it?

+2
source share

All Articles