Add watermark to PDF file

sahin kaya 20 Reputation points
2023-10-07T18:06:03.3566667+00:00

Hello

I tried to adapt the codes below from the internet, but as a result I get a PDF printout without watermark. I wonder where the error is. source code > https://www.pdfsharp.net/wiki/Watermark-sample.ashx

 string watermark = "DENEME";             // Get an XGraphics object for drawing beneath the existing content.             PdfDocument destDocument = PdfSharp.Pdf.IO.PdfReader.Open(@"D:\test\cıktı.pdf", PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify);             var page = destDocument.Pages[0];             var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);              // Get the size (in points) of the text.             XFont xFontt = new Font(Font.FontFamily, 40, Font.Style, GraphicsUnit.World);             var size = gfx.MeasureString(watermark,xFontt);              // Define a rotation transformation at the center of the page.             gfx.TranslateTransform(page.Width / 2, page.Height / 2);             gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);             gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);              // Create a graphical path.             var path = new XGraphicsPath();              // Create a string format.             var format = new XStringFormat();             format.Alignment = XStringAlignment.Near;             format.LineAlignment = XLineAlignment.Near;              // Add the text to the path.             // AddString is not implemented in PDFsharp Core.             path.AddString(watermark, xFontt.FontFamily, XFontStyle.BoldItalic, 150,             new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),                 format);              // Create a dimmed red pen.             var pen = new XPen(XColor.FromArgb(128, 255, 0, 0), 2);              // Stroke the outline of the path.             gfx.DrawPath(pen, path);              //destDocument.AddPage(gfx.PdfPage);             destDocument.Save("d:/test/yeni1.pdf"); 
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,404 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,756 Reputation points
    2023-10-07T18:47:09.0333333+00:00

    Two things I've noticed:

    var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
    

    Here XGraphicsPdfPageOptions.Append will draw your watermark behind the original page, so if the original page is opaque then you won't see it.

    Also I don't think you need this line:

    destDocument.AddPage(gfx.PdfPage);
    

    As the XGraphics context you're drawing to is already drawing to the original page, so once you .Save() the PdfDocument it will affect the original page. .AddPage() will just add a new page to the document that's a duplicate of the page you're drawing on.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.