0

I have been trying to position a Text Water marks on PDF file using iTextSharp and i am struggling to find the coordinates on each page. it works fine when all the pages in pdf file are of same rotation but if the rotation is different then the coordinates are completely different.

PdfImportedPage page = stamper.GetImportedPage(pdfReader, i);
var rotationValue = page.Rotation;

and to add the watermark

cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is WaterMark 1", 20, 20, 90f);

this code is unable to find the X and Y position on the page. how can i get the exact location where i want to add the watermark?

John Sheedy
  • 287
  • 1
  • 8
  • 26

1 Answers1

0

This question is answered in an article written in French that was based on several StackOverflow questions I answered in English: Comment créer un filigrane transparent en PDF?

The questions this blog post was based on, are:

These questions and their answers can be found in The Best iText Questions on StackOverflow, a free ebook that can be downloaded from the iText site. It also contains a couple of answers that were never published on StackOverflow.

You shouldn't import the page to find out the rotation. There are other ways to get that information. You'll notice that you can use the getPageSize() and the GetPageSizeWithRotation() methods depending on whether or not you want to get the page size along with the rotation (there's also a GetRotation() method).

Furthermore, you should experiment with the RotateContents property:

stamper.RotateContents = false;

It is not exactly clear to me whether or not you want the watermark to follow or ignore the rotation, but the GetPageSize() and the GetPageSizeWithRotation() method, you'll be able to avoid having to use hard-coded values such as x = 20; y = 20 (as done in your code snippet). If you want the middle coordinate of page i, you can use this code:

Rectangle pagesize = reader.GetPageSizeWithRotation(i);
x = (pagesize.Left + pagesize.Right) / 2;
y = (pagesize.Top + pagesize.Bottom) / 2;
Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks, but the problem is i have to add multiple watermarks on each page and when page rotation changes these cordinates. and gap between each watermark is changed – John Sheedy Jun 12 '15 at 09:43
  • Why is that a problem? – Bruno Lowagie Jun 12 '15 at 10:02
  • Let me rephrase my question: what do you mean by *gap between each watermark is changed*? When you add multiple watermarks, **you** decide which coordinates are to be used and as such **you** define that gap, don't you? Also: if it's your intention to create a multi-line watermark, then why are you using `ShowTextAligned()`? – Bruno Lowagie Jun 12 '15 at 10:47
  • its a vertical line same size as the height of the each page and that vertical line has 6 text water marks. if i dont use showTextAligned(), what shall i use for this? – John Sheedy Jun 12 '15 at 11:05