2

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 position of text, and 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.

Thanks!

euler
  • 1,401
  • 2
  • 18
  • 39

1 Answers1

2

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.

Regarding the fact that only a portion of the overlay text is visible: my first guess was that you're adding content under the existing content, but that guess is wrong (you're using getOverContent()). What exactly do you mean by that second question? Do yo mean the text is clipped by the CropBox? Are you looking for a way to measure the content of phrase to see if it fits the height before you add it?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • The text is not visible and in some instances shifted. I would just like to know the x, y coordinates so that I can position the overlay text in the same position relative to the pdf's page, i.e., centered vertically and should be near the edge of the visible area of the page (either left or right). Some of the pdfs have different page sizes. In this case, what would be the formula? Thanks. – euler Jul 03 '13 at 08:50
  • 2
    Didn't I answer that question already? If `crop` is the `Rectangle` defining the CropBox (if present) or the MediaBox (if there's no CropBox), then `x = crop.getRight() + xPos` and `y = crop.getBottom() + crop.getHeight() / 2`, where `xPos` defines the offset of the text in user units counted from the right border. – Bruno Lowagie Jul 03 '13 at 08:53
  • According to your post here: [http://support.itextpdf.com/node/105](http://support.itextpdf.com/node/105), you said "When you add content at an absolute position, you'll need to take the (x,y) value of the origin into account if it’s different from (0,0). Otherwise, you risk adding content in the wrong place, maybe even outside the visible area of the page." My sincere apologies if I didn't get this right. I tried `crop.getRight() + xPos, crop.getBottom() + crop.getHeight() / 2` but still, the position of the content that I want to add is not consistent. – euler Jul 03 '13 at 10:16
  • Thanks @bruno-lowagie. Please take a look at this generated pdf:[http://repository.seafdec.org.ph/easysubmit/infotips_mangrove_friendly4.pdf](http://repository.seafdec.org.ph/easysubmit/infotips_mangrove_friendly4.pdf). The overlay text is consistent from page 1-3 and 20-21 but its outside the visible area in the rest of the pages. Hope you can help me. Thanks in advance once again. – euler Jul 03 '13 at 10:51
  • I've looked at the PDF and found your error: you're assuming that every page has the same size. You should get the CropBox value for every page! – Bruno Lowagie Jul 03 '13 at 13:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32832/discussion-between-euler-and-bruno-lowagie) – euler Jul 03 '13 at 14:19
  • I hate chat because it drains away precious time. Asking me to go to the chat is the equivalent of me asking you to create a ticket on our paid support ticketing system (only paying customers can do that). Anyway: your error was so obvious everybody overlooked it: you're always getting the crop box for page 1: getCropBox(**1**) – Bruno Lowagie Jul 03 '13 at 14:29
  • 1
    Thank you so much for the time and pointing out what I have overlooked. I will just try to figure out the offset position or whatever by trial and error. I still have a lot to read from your book. My sincere apologies for using the chat feature. Thanks and regards. – euler Jul 03 '13 at 15:08