首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PDFsharp水印

PDFsharp水印
EN

Stack Overflow用户
提问于 2017-08-07 16:48:19
回答 1查看 4.7K关注 0票数 8

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

我使用的是PDFsharp版本1.50.4000

代码语言:javascript
复制
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;
        }

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-07 20:34:22

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

document.Save循环之外调用forProcess.Start

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

PDFsharp源代码包括一个水印示例:

http://pdfsharp.net/wiki/Watermark-sample.ashx

在现有PDF页面上添加半透明文本的有两个变体。这些变体也适用于没有透明度的PDF页面。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45551941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档