【问题标题】:Add a Text Watermark using Stamper on PDFTron UWP在 PDFTron UWP 上使用 Stamper 添加文本水印
【发布时间】:2018-03-07 05:34:58
【问题描述】:

我使用以下代码在我的 pdf 中添加了水印。我的实际要求是在用户尝试打印 pdf 时显示水印。 (打印预览和打印的副本应该有水印而不是 在查看器上的原始文档上)

我设置了 ShowsOnScreen(false) 和 ShowsOnPrint(true) 标志,但它会显示在打印预览上,但也会显示在查看器上。 另一个问题是可以选择此水印并进行其他注释,例如突出显示。

请给我一个解决方法。我需要向查看器隐藏水印并仅在打印预览/打印时显示 或者 向我展示一种水印也显示在查看器上的方式,但它必须是只读的。

using (Stamper st = new Stamper(StamperSizeType.e_relative_scale, 0.9, 0.9)) 
                { 
                    pdfDoc.InitSecurityHandler(); 

                    st.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_center); 
                    st.SetFontColor(new ColorPt(0, 0, 0));                    st.SetOpacity(0.3); 
                    st.SetAsBackground(false); 
                    st.ShowsOnScreen(false); 

                    st.SetRotation(-45); 
                    st.ShowsOnPrint(true); 

                    st.SetAsAnnotation(false); 

                    st.StampText(pdfDoc, "If you are reading this\nthis is an even page", new PageSet(1, pdfDoc.GetPageCount())); 
                }

【问题讨论】:

    标签: uwp win-universal-app watermark pdftron


    【解决方案1】:

    在 PDFTron 团队的帮助下进行了排序。这是代码

    public async Task<PDFDoc> SetWatermark(PDFDoc pdfDoc)
        {
            try
            {
                #region Solution 1
                var text = "Sample Watermark\nSample Watermark";
    
                pdfDoc.InitSecurityHandler();
                Image img1 = null;
    
                // Create a dummy document
                using (PDFDoc tempDoc = new PDFDoc())
                using (Stamper st = new Stamper(StamperSizeType.e_relative_scale, 0.9, 0.9))
                {
            // Add a page to the dummy doc.
                    tempDoc.PagePushBack(tempDoc.PageCreate());
                    st.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_center);
    
                    // Set text color to black
                    st.SetFontColor(new ColorPt(0, 0, 0));
    
                    // Add a text to the dummy page. This text will be the watermark you going to apply on your original document.
                    st.StampText(tempDoc, text, new PageSet(1, tempDoc.GetPageCount()));
            // Reduce PDF page to minimal size
                    tempDoc.GetPage(1).SetMediaBox(tempDoc.GetPage(1).GetVisibleContentBox());
    
            // Require a sepaprate license
                    PDFDraw draw = new PDFDraw();
                    // Adjust image output quality
                    draw.SetDPI(100);
                    draw.SetPageTransparent(true);
    
                    // Export the dummy page's text as an image
                    var pngImageData = await draw.ExportAsStreamAsync(tempDoc.GetPage(1), "PNG");
                    img1 = await Image.CreateAsync(pdfDoc.GetSDFDoc(), pngImageData);
                }
    
                // Create a new stamper to embed the above created image to the original doc
                using (Stamper st = new Stamper(StamperSizeType.e_relative_scale, 0.9, 0.9))
                {
                    // Adjust final stamp positioning
                    st.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_center);
                    st.SetOpacity(0.3);
                    st.SetAsBackground(false);
                    st.ShowsOnScreen(true);
                    st.SetRotation(-45);
                    st.ShowsOnPrint(true);
                    st.SetAsAnnotation(false); //Make it true if you want to add this watermark to the XFDF file
    
                    st.StampImage(pdfDoc, img1, new PageSet(1, pdfDoc.GetPageCount()));
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw;
            }
            return pdfDoc; //Retrun the watermark embeded document
        }
    

    【讨论】:

    • 感谢您的建议。
    • 顺便说一句,您可以设置权限以限制在某些编辑器(例如 Adob​​e Acrobat)中编辑您的 PDF。您所需要的只是定义一个新的 StdSecurityHandler(用于 .NET FW),然后为 PDFDoc void SetPermissions(PDFDoc doc) { var securityHandler = new StdSecurityHandler(); securityHandler.SetPermission(SecurityHandler.Permission.e_doc_modify, false); doc.SetSecurityHandler(securityHandler); } 设置处理程序
    • @DamirBeylkhanov 谢谢。我只希望注释是只读的。不是页面。
    猜你喜欢
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多