在 PDFTron UWP 上使用 Stamper 添加文本水印
Add a Text Watermark using Stamper on PDFTron UWP
我使用以下代码在我的 pdf 中添加了水印。我的实际要求是在用户尝试打印 pdf 时显示水印。 (打印预览和打印件应有水印,无
在查看器上的原始文档上)
我设置了 ShowsOnScreen(false) 和 ShowsOnPrint(true) 标志,但它显示在打印预览中,但也显示在查看器中。
另一个问题是可以选择此水印并进行其他注释,例如突出显示。
请建议我解决方法。要么我需要对查看器隐藏水印并仅在打印时显示 preview/print
要么
告诉我一种水印在查看器上显示的方式,但它必须是只读的。
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
}
我使用以下代码在我的 pdf 中添加了水印。我的实际要求是在用户尝试打印 pdf 时显示水印。 (打印预览和打印件应有水印,无 在查看器上的原始文档上)
我设置了 ShowsOnScreen(false) 和 ShowsOnPrint(true) 标志,但它显示在打印预览中,但也显示在查看器中。 另一个问题是可以选择此水印并进行其他注释,例如突出显示。
请建议我解决方法。要么我需要对查看器隐藏水印并仅在打印时显示 preview/print 要么 告诉我一种水印在查看器上显示的方式,但它必须是只读的。
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
}