在PDFTron UWP上使用压模添加文本水印

在PDFTron UWP上使用压模添加文本水印,uwp,win-universal-app,watermark,pdftron,Uwp,Win Universal App,Watermark,Pdftron,我已使用以下代码将水印添加到我的pdf中。我的实际需求是在用户尝试打印pdf时显示水印。(打印预览和打印副本应具有水印,而不是 在查看器上的原始文档上) 我设置了标志ShowsOnScreen(false)和ShowsOnPrint(true),但它会显示在打印预览上,也会显示在查看器上。 另一个问题是可以选择此水印并进行其他注释,如高亮显示 请给我建议一个解决办法。或者我需要对查看器隐藏水印,并仅在打印预览/打印时显示 或 向我展示水印在查看器上的显示方式,但它必须是只读的 using (St

我已使用以下代码将水印添加到我的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())); 
                }

在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
    }
公共异步任务集水印(PDFDoc PDFDoc)
{
尝试
{
#区域解决方案1
var text=“样本水印\n样本水印”;
pdfDoc.InitSecurityHandler();
图像img1=null;
//创建虚拟文档
使用(PDFDoc tempDoc=new PDFDoc())
使用(压模st=新压模(压模类型e_相对刻度,0.9,0.9))
{
//向虚拟文档添加页面。
tempDoc.PagePushBack(tempDoc.PageCreate());
st.SetAlignment(冲压件水平对准.e_水平对准中心,冲压件垂直对准.e_垂直对准中心);
//将文本颜色设置为黑色
st.SetFontColor(新颜色PT(0,0,0));
//向虚拟页面添加文本。此文本将是您要应用于原始文档的水印。
st.StampText(tempDoc,text,新页面集(1,tempDoc.GetPageCount());
//将PDF页面缩小到最小大小
SetMediaBox(tempDoc.GetPage(1.GetVisibleContentBox());
//需要独立许可证
PDFDraw draw=新的PDFDraw();
//调整图像输出质量
绘制。设置dpi(100);
draw.SetPageTransparent(true);
//将虚拟页的文本导出为图像
var pngImageData=await draw.ExportAsStreamAsync(tempDoc.GetPage(1),“PNG”);
img1=await Image.CreateAsync(pdfDoc.GetSDFDoc(),pngImageData);
}
//创建一个新的压模,将上面创建的图像嵌入到原始文档中
使用(压模st=新压模(压模类型e_相对刻度,0.9,0.9))
{
//调整最终压印位置
st.SetAlignment(冲压件水平对准.e_水平对准中心,冲压件垂直对准.e_垂直对准中心);
st.SetOpacity(0.3);
st.SetAsBackground(假);
圣ShowsOnScreen(真实);
圣塞特旋转(-45);
圣肖森印刷(真实);
st.SetAsAnnotation(false);//如果要将此水印添加到XFDF文件,请将其设置为true
st.StampImage(pdfDoc,img1,新页面集(1,pdfDoc.GetPageCount());
}
#端区
}
捕获(例外情况除外)
{
投掷;
}
return pdfDoc;//重新运行嵌入水印的文档
}

谢谢您的建议。顺便说一句,您可以在某些编辑器(例如Adobe Acrobat)中设置权限以限制对PDF的编辑。您只需定义一个新的StdSecurityHandler(用于.NET FW),然后为PDFDoc
void SetPermissions(PDFDoc doc){
var securityHandler=new StdSecurityHandler();
securityHandler.SetPermission(securityHandler.Permission.e_doc_modify,false);
}
@DamirBeylkhanov谢谢。我只希望注释是只读的。不是那页。