【问题标题】:PDFsharp WatermarkPDFsharp水印
【发布时间】:2017-08-07 16:48:19
【问题描述】:

我正在制作一个在用户选择的 PDF 上创建水印的应用程序,我似乎无法让水印出现在所选 PDF 上,但我也没有收到任何错误。任何帮助,将不胜感激。

我使用的是 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;
        }

    }

【问题讨论】:

    标签: c# pdfsharp


    【解决方案1】:

    也许试试XGraphicsPdfPageOptions.Append而不是XGraphicsPdfPageOptions.Prepend

    for 循环之外调用document.SaveProcess.Start

    更新:说明:使用XGraphicsPdfPageOptions.Prepend,水印绘制在原始 PDF 页面下方。大多数 PDF 文件由透明背景上的黑色文本组成,并且水印在那里可见(您可以通过激活 Adob​​e Reader 中的透明网格来检查这一点)。对于具有纯色背景的 PDF 页面(例如图像、具有背景颜色的表格等),水印将不可见。

    PDFsharp 源代码包含一个水印示例:
    http://pdfsharp.net/wiki/Watermark-sample.ashx
    有两种变体可以在现有 PDF 页面的顶部添加半透明文本。这些变体也适用于不透明的 PDF 页面。

    【讨论】:

    • 谢谢,确实需要追加。
    • 感谢您的反馈。更新了我的答案:添加了解释,现在问题已经解决了。
    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    相关资源
    最近更新 更多