Hello,
I'm using your PDF component to print a text watermark ("CONFIDENTIAL") on a page and I've followed your tutorial at https://help.syncfusion.com/file-formats/pdf/working-with-watermarks
It works but I have the following issues:
1) I don't know how many pages is my PDF file, so I need to print the watermark on every page
2) I need the text watermark to be vertically and horizontally centered on the page. I cannot use a fixed position (like in your example) because the page may be in Portrait or Landscape orientation, A4 or A3 size, etc.). How can I center it regardless of page size, orientation etc.?
Thanks!
Hi Paolo,
We have created a sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PDF_SAmple_WatermarkAtCenter154317391 to achieve your requirement of adding watermark to be vertically and horizontally centered on all pages in the PDF document.
Kindly try the attached sample and let us know the result.
Regards,
Surya V
Thanks, it works! But I want to rotate it, so I use:
graphics.RotateTransform(-45);
and it's not centered any longer. In other words, I need the rotation to be from the center, not from the top-left of the rectangle.
Thanks!
Hi Paolo,
We have modified the sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PDF_SAmple_WM_WithRotateCenter-1891574735 accordingly for graphics rotation of -45degree. We recommend you modify the drawstring of graphics bounds based on their rotation. For example, if graphics rotated to -45degree, then we need to find the height of graphics by using diagonal (which can be expressed with the following formula: d² = l² + w² ). Refer to the following code snippet,
|
//Create graphics for the existing page PdfGraphics graphics = loadedPage.Graphics; float x = size.Width / 2 - loadedPage.Size.Width / 2; float y = (float)Math.Sqrt((loadedPage.Size.Width * loadedPage.Size.Width) + (loadedPage.Size.Height * loadedPage.Size.Height)) / 2f;
//Insert watermark text to PDF PdfGraphicsState state = graphics.Save(); graphics.SetTransparency(0.25f); float angle = -45; graphics.RotateTransform(angle); x += (angle / 2); graphics.DrawString(watermarkText, font, PdfBrushes.Red, new RectangleF(x, y, size.Width, size.Height)); graphics.Restore(); |
Kindly try the attached sample and let us know the result.
Regards,
Surya V
Hello,
I tried it and I'm sorry it does not work. Or, better, it works but the watermark is not centered (I need it to be fully centered both vertically and horizontally). You can better see it if:
Hi Paolo,
Thanks for your update. We have modified a sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PDF_SAmple_WaterMarkWithRotateCenter-830511359 to achieve your requirement of adding watermark to be vertically and horizontally centered on all pages in the PDF document. The attached sample generated output documents for your reference,
|
Short text output |
Long Text output |
|
|
|
Kindly try the attached sample and let us know the result.
Regards,
Surya V
Yes, it works, thank you!