简体   繁体   English

使用 iTextSharp 创建时将图像水印添加到 Pdf

[英]Adding Image watermark to Pdf while Creating it using iTextSharp

Wonder if this possible.想知道这是否可能。 Saw many posts on adding watermark after the pdf is created and saved in disk.在创建 pdf 并将其保存在磁盘中后,看到许多关于添加水印的帖子。 But during creation of document how do i add a image watermark.但是在创建文档期间如何添加图像水印。 I know how to add a image to document.我知道如何将图像添加到文档中。 But how do i position it such that it comes at the end of page.但是我如何定位它以使其出现在页面的末尾。

For C#, use this code...对于 C#,请使用此代码...

//new Document

Document DOC = new Document();


// open Document

DOC.Open();


//create New FileStream with image "WM.JPG"

FileStream fs1 = new FileStream("WM.JPG", FileMode.Open);


iTextSharp.text.Image JPG = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(fs1), ImageFormat.Jpeg);


//Scale image

JPG.ScalePercent(35f);


//Set position

JPG.SetAbsolutePosition(130f,240f);

//Close Stream

fs1.Close();


DOC.Add(JPG);

This is essentially identical to adding a header or footer.这与添加页眉或页脚基本相同。

You need to create a class that implements PdfPageEvent , and in the OnPageEnd , grab the page's PdfContentByte, and draw your image there.您需要创建一个实现PdfPageEvent的类,并在OnPageEnd ,获取页面的 PdfContentByte,并在那里绘制图像。 Use an absolute position.使用绝对位置。

Note: You probably want to derive from PdfPageEventHelper, it has empty implementations of all the page events, so you just need to write the method you actually care about.注意:您可能想从 PdfPageEventHelper 派生,它具有所有页面事件的空实现,因此您只需要编写您真正关心的方法。

Note: Unless your image is mostly transparent, drawing it on top of your page will cover up Many Things.注意:除非您的图像大部分是透明的,否则将其绘制在页面顶部会掩盖许多事情。 IIRC ("If I Recall Correctly"), PNG and GIF files added by iText will automatically be properly masked, allowing things under them to show through. IIRC(“如果我记得正确”),iText 添加的 PNG 和 GIF 文件将自动被正确屏蔽,让它们下面的东西显示出来。

If you want to add an opaque image underneath everything, you should override OnStartPage() instead.如果您想在所有内容下面添加一个不透明的图像,您应该重写OnStartPage()

This is Java, but converting it is mostly a matter of capitalizing method names and swapping get/set calls for property access.这是 Java,但转换它主要是将方法名称大写并交换 get/set 调用以进行属性访问。

Image watermarkImage = new Image(imgPath);
watermarkImage.setAbsolutePosition(x, y);

writer.setPageEvent( new MyPageEvent(watermarkImage) );


public MyPageEvent extends PdfPageEventHelper {
  private Image waterMark;
  public MyPageEvent(Image img) {
    waterMark = img;
  }
  public void OnEndPage/*OnStartPage*/(PdfWriter writer, Document doc) {
    PdfContentByte content = writer.getContent();
    content.addImage( waterMark );
  }
}

This is the accepted answer's port to C#, and what worked for me.这是已接受的 C# 答案的端口,对我有用。 I'm using an A4 page size:我使用的是 A4 页面大小:

Define this BackgroundImagePdfPageEvent class:定义这个BackgroundImagePdfPageEvent类:

public class BackgroundImagePdfPageEvent : PdfPageEventHelper
{
    private readonly Image watermark;

    public BackgroundImagePdfPageEvent(string imagePath)
    {
        using (var fs = new FileStream(imagePath, FileMode.Open))
        {
            watermark = Image.GetInstance(System.Drawing.Image.FromStream(fs), ImageFormat.Jpeg);
            watermark.SetAbsolutePosition(0, 0);
            watermark.ScaleAbsolute(PageSize.A4.Width, PageSize.A4.Height);
            watermark.Alignment = Image.UNDERLYING;
        }
    }

    public override void OnStartPage(PdfWriter writer, Document document)
    {
        document.Add(watermark);
    }
}

Then, when creating your document:然后,在创建文档时:

var doc = new Document(PageSize.A4);
doc.SetMargins(60f, 60f, 120f, 60f);
var outputStream = new MemoryStream();
var writer = PdfWriter.GetInstance(doc, outputStream);
var imagePath = "PATH_TO_YOUR_IMAGE";
writer.PageEvent = new BackgroundImagePdfPageEvent(imagePath);

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

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