简体   繁体   English

PDFsharp水印

[英]PDFsharp Watermark

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. 我正在制作一个在用户选择的PDF上创建水印的应用程序,但似乎无法使水印显示在所选PDF上,但我也没有收到任何错误。 Any help would be appreciated. 任何帮助,将不胜感激。

I am using PDFsharp version 1.50.4000 我正在使用PDFsharp版本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;
        }

    }

Maybe try XGraphicsPdfPageOptions.Append instead of XGraphicsPdfPageOptions.Prepend . 也许尝试XGraphicsPdfPageOptions.Append而不是XGraphicsPdfPageOptions.Prepend

Call document.Save and Process.Start outside the for loop. for循环外调用document.SaveProcess.Start

Update: Explanation: With XGraphicsPdfPageOptions.Prepend the watermark is drawn below the original PDF page. 更新:说明:使用XGraphicsPdfPageOptions.Prepend ,水印被绘制在原始PDF页面下方。 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). 大多数PDF文件由透明背景上的黑色文本组成,并且水印将在那里可见(您可以通过在Adobe Reader中激活“透明网格”进行检查)。 For PDF pages with a solid background (eg images, tables with a background colour, ...) the watermark will not be visible. 对于具有纯色背景的PDF页面(例如,图像,具有背景色的表格...),水印将不可见。

The PDFsharp source code includes a Watermark sample: PDFsharp源代码包括一个水印示例:
http://pdfsharp.net/wiki/Watermark-sample.ashx http://pdfsharp.net/wiki/Watermark-sample.ashx
There are two variants that add a semi-transparent text on top of the existing PDF page. 有两种变体可以在现有PDF页面顶部添加半透明文本。 These variants also work for PDF pages without transparency. 这些变体也适用于不透明的PDF页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM