How to add a watermark to a PDF file?

后端 未结 2 645
生来不讨喜
生来不讨喜 2020-11-30 11:29

I\'m using C# and iTextSharp to add a watermark to my PDF files:

Document document = new Document();
PdfReader pdfReader = new PdfReader(strFileLocation);
Pd         


        
2条回答
  •  悲&欢浪女
    2020-11-30 12:05

    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?

    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.

提交回复
热议问题