logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Paul Rayman  
#1 Posted : Sunday, May 21, 2017 4:48:51 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Question:
How can I add a watermark when printing a document?
The watermark content should by dynamic (usually it's the UserName).

Answer:
You should create a derived class from PdfPrintDocument and override the OnPrintPage() method.
Inside that method you can get access to Graphics to draw your watermark.
There is you have two options.
Please look at code below
Code:

    public class PdfPrintDocumentEx : PdfPrintDocument
    {
        public PdfPrintDocumentEx(PdfDocument Document, int mode = 0) : base(Document, mode)
        {
        }

        //Draw watermark in the background (under page content)
        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            //Draw watermark
            e.Graphics.FillRectangle(Brushes.Red, 0, 0, 500, 500);
            //Draw page content
            base.OnPrintPage(e);
        }

        //Draw watermark above page content (with transparency)
        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            //Draw page content
            base.OnPrintPage(e);
            //Draw watermark
            e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, 255, 0, 0)), 0, 0, 500, 500);
        }
    }


and printing...

Code:

     var doc = PdfDocument.Load(@"D:/sample.pdf");

     var pd = new PdfPrintDocumentEx(doc);
     pd.Print();


Please note, when you drawing watermark in the background it may becomes invisible due to page content is not transparency (scanned pages for example)

Edited by user Sunday, May 21, 2017 4:51:31 PM(UTC)  | Reason: Not specified

msg  
#2 Posted : Monday, May 22, 2017 12:21:43 AM(UTC)
msg

Rank: Newbie

Groups: Registered
Joined: 5/22/2017(UTC)
Posts: 3

Thanks: 1 times
Hi,
Thanks for the quick reply.
Where can I find more information on the dealing with graphics ?

Thanks
Paul Rayman  
#3 Posted : Monday, May 22, 2017 5:44:00 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Originally Posted by: msg Go to Quoted Post
Where can I find more information on the dealing with graphics ?


here: https://msdn.microsoft.c...em.drawing.graphics.aspx
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.