Print Watermark

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!



7 Replies

SV Surya Venkatesan Syncfusion Team February 25, 2022 06:48 PM UTC

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



PA Paolo February 25, 2022 10:27 PM UTC

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!



SV Surya Venkatesan Syncfusion Team replied to Paolo February 28, 2022 07:00 PM UTC

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



PA Paolo February 28, 2022 10:15 PM UTC

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:


  • You use a shorter watermark, for example 'PREVIEW'
  • You use the attached PDF file (which is mostly blank)

If you try your code with the attached PDF file and 'PREVIEW' as watermark, you'll get the attached output (see attached) and, as you can see, it's not centered.

Attachment: Watermark_rotate_c7cd9e2a.zip


SV Surya Venkatesan Syncfusion Team replied to Paolo March 1, 2022 06:46 PM UTC

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



PA Paolo March 1, 2022 07:28 PM UTC

Yes, it works, thank you!



GK Gowthamraj Kumar Syncfusion Team March 2, 2022 08:45 AM UTC

Hi Paolo, 
  
Thank you for your update. Please let us know if you need any further assistance in this. 
 
Regards, 
Gowthamraj K 


Loader.
Up arrow icon