PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 8:42 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Pdf file as watermark
PostPosted: Wed Mar 25, 2020 2:50 pm 
Offline

Joined: Wed Mar 25, 2020 2:34 pm
Posts: 5
Hi everyone!

I'd like to thank first and introduce myself. I'll help in everything i can, but now i need some help, i'm newbie with this tool.

I'm trying to use a pdf (single page) as a watermark for another pdf (many pages).

I can achieve this but the issue is the way it draws the image. I need the watermark to be underneath de main document pages, not above, as this method is doing :

Code:
using (PdfSharp.Pdf.PdfDocument originalDocument = PdfReader.Open(@"C:\Users\...\Merged.pdf", PdfDocumentOpenMode.Import))
            using (PdfSharp.Pdf.PdfDocument outputPdf = new PdfSharp.Pdf.PdfDocument())
            {

                foreach (PdfSharp.Pdf.PdfPage page in originalDocument.Pages)
                {
                                     
                    outputPdf.AddPage(page );
                   
                }


                var background = XImage.FromFile(@"C:\Users\....\background.pdf");
                foreach (PdfSharp.Pdf.PdfPage page in outputPdf.Pages)
                {
                    XGraphics graphics = XGraphics.FromPdfPage(page);
                    graphics.DrawImage(background, 1, 1);

                }

                outputPdf.Save(@"C:\Users\.....\Final.pdf");
            }



Any one can help?
thak you so much.

best regards.


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 25, 2020 5:52 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
The answer can be found in the official Watermark sample:

Code:
// Get an XGraphics object for drawing beneath the existing content.
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);


Use "XGraphicsPdfPageOptions.Prepend", that should do the magic.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 25, 2020 6:41 pm 
Offline

Joined: Wed Mar 25, 2020 2:34 pm
Posts: 5
Hi Thomas,

Thank you so much for answering!!
When i use : XGraphicsPdfPageOptions.Prepend the watermark doesn't show, but when XGraphicsPdfPageOptions is set to append, it shows but above, the same result as before.

Any idea?

thanks!

best regards


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 25, 2020 7:00 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
"Prepend" works only for the transparent areas of a PDF page. For most PDFs the "whitespace" is transparent. You can check this when you activate the transparency grid in Adobe Reader.
Some PDFs do not have any transparent areas, for example scanned documents. They will completely hide prepended watermarks. Game lost.

Use a transparent or semi-transparent watermark and append that.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 26, 2020 3:25 pm 
Offline

Joined: Wed Mar 25, 2020 2:34 pm
Posts: 5
Hi Thomas,

Hope you are well,

I remade main pdf forcing transparent background and it worked flawlessly. Great!

Thank you very much for your support!

kind regards,

Carles.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot], travrance and 145 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group