【问题标题】:Add Watermark to PDF using iTextSharp Not working for Image PDF C#使用 iTextSharp 向 PDF 添加水印不适用于图像 PDF C#
【发布时间】:2021-04-14 07:20:44
【问题描述】:

我正在使用以下代码在 PDF 中添加水印。它适用于普通文本 PDF。如果包含图片水印是不可见的。

public void AddWatermarkPdf()
      {
          PdfReader pdfReader = new PdfReader("SimpleArabic.pdf");
          PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(DateTime.Now.ToFileTime() 
         + "Out.pdf", FileMode.Create, FileAccess.Write, FileShare.None));
         iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("WaterMarkDoc.png");
          img.SetAbsolutePosition(-200, -50);
          PdfContentByte waterMark;
          for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
          {
            Rectangle pagesize = pdfReader.GetCropBox(pageIndex);
            waterMark = pdfStamper.GetUnderContent(pageIndex);
            waterMark.AddImage(img);
          }
          pdfStamper.FormFlattening = true;
          pdfStamper.Close();
     }
   

即使图像是 PDF 格式,也必须应用水印。

【问题讨论】:

  • 您的代码不会添加水印,它会在任何现有内容的后面添加图像。图像上方显示的任何不透明对象(如另一个图像)都会隐藏它。使您的图像显示在另一个图像下方的唯一方法是使现有图像透明

标签: c# .net pdf itext


【解决方案1】:

添加水印的方法有两种:在现有内容下或在现有内容之上。出于您的预期目的,最好的方法是在 PDF 内容的顶部添加水印,并将水印设置为较低的不透明度。

这是一个 Java 代码示例,但可以很容易地转录:

protected void manipulatePdf(String dest) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
    PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.HELVETICA));
    PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
    over.setFillColor(ColorConstants.BLACK);
    Paragraph p = new Paragraph("This watermark is added ON TOP OF the existing content")
            .setFont(font).setFontSize(15);
    new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
            .showTextAligned(p, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
    p = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
            .setFont(font).setFontSize(15);
    over.saveState();
    PdfExtGState gs1 = new PdfExtGState();
    gs1.setFillOpacity(0.5f);
    over.setExtGState(gs1);
    new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
             .showTextAligned(p, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
    over.restoreState();
    pdfDoc.close();
}

来源:这在 iText 知识库中有非常详尽的解释:https://kb.itextpdf.com/home/it7kb/faq/how-to-add-a-watermark-to-a-page-with-an-opaque-image

编辑:

这是您正在使用的 iText 5 版本的 C# 示例:

 public class TransparentWatermark 
    {
        public static readonly String DEST = "results/sandbox/stamper/transparent_watermark.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hero.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new TransparentWatermark().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfCanvas under = new PdfCanvas(pdfDoc.GetFirstPage().NewContentStreamBefore(), new PdfResources(), pdfDoc);
            PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(StandardFonts.HELVETICA));
            Paragraph paragraph = new Paragraph("This watermark is added UNDER the existing content")
                    .SetFont(font)
                    .SetFontSize(15);
            
            Canvas canvasWatermark1 = new Canvas(under, pdfDoc.GetDefaultPageSize())
                    .ShowTextAligned(paragraph, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark1.Close();
            PdfCanvas over = new PdfCanvas(pdfDoc.GetFirstPage());
            over.SetFillColor(ColorConstants.BLACK);
            paragraph = new Paragraph("This watermark is added ON TOP OF the existing content")
                    .SetFont(font)
                    .SetFontSize(15);
            
            Canvas canvasWatermark2 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                    .ShowTextAligned(paragraph, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark2.Close();
            paragraph = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
                    .SetFont(font)
                    .SetFontSize(15);
            over.SaveState();
            
            // Creating a dictionary that maps resource names to graphics state parameter dictionaries
            PdfExtGState gs1 = new PdfExtGState();
            gs1.SetFillOpacity(0.5f);
            over.SetExtGState(gs1);
            Canvas canvasWatermark3 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                    .ShowTextAligned(paragraph, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark3.Close();
            over.RestoreState();
            
            pdfDoc.Close();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 2018-07-12
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多