Watermark every page

Hi 

I have used the following code which works fine, but it only watermarks the first page and I need it to watermark every page, I cant find any examples that watermarks every page of the document. 

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileName);

            PdfPageBase loadedPage = loadedDocument.Pages[0];

            PdfGraphics graphics = loadedPage.Graphics;

            //set the font

            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            // watermark text.

            PdfGraphicsState state = graphics.Save();

            graphics.SetTransparency(0.50f);

            graphics.RotateTransform(-0);

            graphics.DrawString("Imported using Essential PDF", font, PdfPens.Red, PdfBrushes.Red, new PointF(20, 20));

            //Save and close the document.

            loadedDocument.Save(destfile);

            loadedDocument.Close(true);

3 Replies

SL Sowmiya Loganathan Syncfusion Team May 8, 2020 11:56 AM UTC

Hi Allan,   
  
Thank you for contacting Syncfusion support.   
  
We can able to add watermark in all pages of an existing PDF document. Please refer the below code snippet for your reference,   
  
//Load the PDF document  
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf");  
  
//Set the font  
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);  
  
foreach (var page in loadedDocument.Pages)  
{  
    //Get the loaded page  
    PdfLoadedPage loadedPage = page as PdfLoadedPage;  
  
    //Create graphics   
    PdfGraphics graphics = loadedPage.Graphics;  
  
    //Watermark text  
    PdfGraphicsState state = graphics.Save();  
    graphics.SetTransparency(0.25f);  
    graphics.RotateTransform(-40);  
  
    graphics.DrawString("Imported using Essential PDF", font, PdfPens.Red, PdfBrushes.Red,new PointF(-150, 450));  
  
    graphics.Restore();  
}  
  
//Save and close the document  
loadedDocument.Save("Watermark.pdf");  
loadedDocument.Close(true);  
  
  
Please try the above solution in your end and let us know the result.   
  
Regards,  
Sowmiya Loganathan  
  



AL Allan May 9, 2020 07:47 AM UTC

Yep worked perfectly, thank you.


PN Preethi Nesakkan Gnanadurai Syncfusion Team May 11, 2020 04:40 AM UTC

Hi Allan, 
  
Most welcome. 
  
Regards, 
Preethi 


Loader.
Up arrow icon