简体   繁体   English

如何在PDF文件中添加水印?

[英]How to add a watermark to a PDF file?

I'm using C# and iTextSharp to add a watermark to my PDF files: 我正在使用C#和iTextSharp在我的PDF文件中添加水印:

Document document = new Document();
PdfReader pdfReader = new PdfReader(strFileLocation);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(strFileLocationOut, FileMode.Create, FileAccess.Write, FileShare.None));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
img.SetAbsolutePosition(100, 300);
PdfContentByte waterMark;
//    
for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
{
    waterMark = pdfStamper.GetOverContent(pageIndex);
    waterMark.AddImage(img);
}
//
pdfStamper.FormFlattening = true;
pdfStamper.Close();

It works fine, but my problem is that in some PDF files no watermark is added although the file size increased, any idea? 它工作正常,但是我的问题是,尽管文件大小增加了,但在某些PDF文件中没有添加水印,您知道吗?

The fact that the file size increases is a good indication that the watermark is added. 文件大小增加这一事实很好地表明已添加水印。 The main problem is that you're adding the watermark outside the visible area of the page. 主要问题是您要在页面的可见区域之外添加水印。 See How to position text relative to page using iText? 请参阅如何使用iText将文本相对于页面放置?

You need something like this: 您需要这样的东西:

Rectangle pagesize = reader.GetCropBox(pageIndex);
if (pagesize == null)
    pagesize = reader.GetMediaBox(pageIndex);
img.SetAbsolutePosition(
    pagesize.GetLeft(),
    pagesize.GetBottom());

That is: if you want to add the image in the lower-left corner of the page. 即:如果要在页面的左下角添加图像。 You can add an offset, but make sure the offset in the x direction doesn't exceed the width of the page, and the offset in the y direction doesn't exceed the height of the page. 您可以添加偏移量,但请确保x方向的偏移量不超过页面的宽度,y方向的偏移量不超过页面的高度。

Although I don't know the specifics of iTextSharp, likely on the pages where your image is not showing, the previous PDF content has modified the current transformation matrix such that whatever you put on the page is moved off the page. 尽管我不知道iTextSharp的详细信息(可能在未显示图像的页面上),但是以前的PDF内容已修改了当前的转换矩阵,因此您在页面上放置的所有内容都移出了页面。

This can be fixed by emitting a gsave operator before the original page content and emitting a grestore operator after the original page content (but before yours). 可以通过在原始页面内容之前发出gsave运算符并在原始页面内容之后(但在您的内容之前)发出grestore运算符来解决此问题。 This, however may not fix all cases with a PDF document that modifies the CTM does a gsave and no grestore. 但是,这可能无法通过修改CTM进行gsave和不使用grestore的PDF文档解决所有情况。 This is not supposed to happen in theory, according to the PDF specification: 根据PDF规范,理论上不应该发生这种情况:

Occurrences of the q and Q operators shall be balanced within a given content stream (or within the sequence of streams specified in a page dictionary's Contents array). q和Q运算符的出现应在给定的内容流中(或在页面字典的Contents数组中指定的流序列中)保持平衡。

but I can tell you from experience that this is not the case in practice. 但我可以根据经验告诉您,实际上并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM