使用 iTextSharp 创建 PDF 时添加图像水印

发布于 2024-11-08 03:41:08 字数 96 浏览 6 评论 0原文

想知道这是否可能。看到很多关于在创建 pdf 并保存在磁盘后添加水印的帖子。但是在创建文档期间如何添加图像水印。我知道如何将图像添加到文档中。但我如何定位它,使其出现在页面末尾。

Wonder if this possible. Saw many posts on adding watermark after the pdf is created and saved in disk. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网白 2024-11-15 03:41:08

对于 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);

For C#, use this code...

//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);
长亭外,古道边 2024-11-15 03:41:08

这本质上与添加页眉或页脚相同。

您需要创建一个实现 PdfPageEvent 的类,并在 OnPageEnd 中获取页面的 PdfContentByte,并在其中绘制图像。使用绝对位置。

注意:您可能想从 PdfPageEventHelper 派生,它具有所有页面事件的空实现,因此您只需要编写您真正关心的方法。

注意:除非您的图像大部分是透明的,否则将其绘制在页面顶部会掩盖很多东西。 IIRC(“如果我没记错的话”),由 iText 添加的 PNG 和 GIF 文件将自动被正确屏蔽,允许它们下面的内容显示出来。

如果您想在所有内容下方添加不透明图像,则应该重写 OnStartPage()

这是 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 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. 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.

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.

If you want to add an opaque image underneath everything, you should override OnStartPage() instead.

This is Java, but converting it is mostly a matter of capitalizing method names and swapping get/set calls for property access.

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 );
  }
}
安稳善良 2024-11-15 03:41:08

这是已接受的答案对 C# 的移植,对我有用。我使用的是 A4 页面尺寸:

定义此 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);
    }
}

然后,在创建文档时:

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);

This is the accepted answer's port to C#, and what worked for me. I'm using an A4 page size:

Define this BackgroundImagePdfPageEvent class:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文