PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Pdf file as watermark
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4118
Page 1 of 1

Author:  kaliand [ Wed Mar 25, 2020 2:50 pm ]
Post subject:  Pdf file as watermark

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.

Author:  TH-Soft [ Wed Mar 25, 2020 5:52 pm ]
Post subject:  Re: Pdf file as watermark

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.

Author:  kaliand [ Wed Mar 25, 2020 6:41 pm ]
Post subject:  Re: Pdf file as watermark

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

Author:  TH-Soft [ Wed Mar 25, 2020 7:00 pm ]
Post subject:  Re: Pdf file as watermark

"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.

Author:  kaliand [ Thu Mar 26, 2020 3:25 pm ]
Post subject:  Re: Pdf file as watermark

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/