PDFsharp Watermark
Asked Answered
S

1

8

I am making an application that creates a watermark on a PDF that the user selects and I can't seem to get the watermark to appear on the selected PDF but I also get no errors. Any help would be appreciated.

I am using PDFsharp version 1.50.4000

public void WaterMarkPDF(string sourceFileName)
    {
        try
        {
            string watermark = "watermark";
            int emSize = 100;
            string file ="test.pdf";

            File.Copy(sourceFileName, file, true);
            File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly);

            // Take in pdf from the form
            PdfDocument document = PdfReader.Open(file);

            // change the version cause sometimes newer versions break it
            if (document.Version < 14)
                document.Version = 14;

            XFont font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);
            for (int idx = 0; idx < document.Pages.Count; idx++)
            {
                var page = document.Pages[idx];

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

                // Get the size (in points) of the text.
                var size = gfx.MeasureString(watermark, font);

                // 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 string format.
                var format = new XStringFormat();
                format.Alignment = XStringAlignment.Near;
                format.LineAlignment = XLineAlignment.Near;

                // Create a dimmed red brush.
                XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));

                // Draw the string.
                gfx.DrawString(watermark, font, brush,
                    new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
                    format);
                // Save the document...
                 document.Save(file);

                // ...and start a viewer.
                Process.Start(file);
            }
        }
        catch (Exception e)
        {
            throw e;
        }

    }
Superorder answered 7/8, 2017 at 16:48 Comment(0)
B
9

Maybe try XGraphicsPdfPageOptions.Appendinstead of XGraphicsPdfPageOptions.Prepend.

Call document.Save and Process.Startoutside the for loop.

Update: Explanation: With XGraphicsPdfPageOptions.Prepend the watermark is drawn below the original PDF page. Most PDF files consist of black text on transparent background and the watermark will be visible there (you can check this by activating the Transparency Grid in Adobe Reader). For PDF pages with a solid background (e.g. images, tables with a background colour, ...) the watermark will not be visible.

The PDFsharp source code includes a Watermark sample:
http://pdfsharp.net/wiki/Watermark-sample.ashx
There are two variants that add a semi-transparent text on top of the existing PDF page. These variants also work for PDF pages without transparency.

Benefaction answered 7/8, 2017 at 20:34 Comment(2)
Thank you, It did need to be append.Superorder
Thanks for the feedback. Updated my answer: Added explanation, now that issue is solved.Benefaction

© 2022 - 2024 — McMap. All rights reserved.