如何图像背景添加到PDF的每一页? [英] How to add image background to pdf for every page?

查看:1043
本文介绍了如何图像背景添加到PDF的每一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个背景到PDF,并设法与我的PDF有一个很大的表,这样的页面不与 Document.NewPage()方法,因此图像背景只在第一页上进行设置。这是code,增加了背景:

I'm trying to set a background to a pdf and managed to set it with an image my pdf has a big table so the pages are added automatically not with the Document.NewPage() method so the image background is set only on the first page. This is the code that adds the background:

    Image backImg = Image.GetInstance(@"D:\websites\DIS\bugs\130208\A4.png");
    backImg.SetAbsolutePosition(0, 0);
    backImg.Alignment = Image.UNDERLYING;

    var doc = new Document(pageSize);
    PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
    doc.Open();

    doc.Add(backImg);
    ...
    creating a big table

而不是使用 doc.NewPage()方法。我一定要扔掉循环每一页,并在年底关闭文档前添加背景图片,但我怎么把它在后台不上其他元素之上?

and not using the doc.NewPage() method. Do I have to loop throw every page and add the background image at the end before closing the doc, but how do I put it in the background not on top of the other elements?

推荐答案

当你想要的东西应用到每一个页面,你应该使用页面事件,更具体<一个href=\"http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPageEvent.html#onEndPage%28com.itextpdf.text.pdf.PdfWriter,%20com.itextpdf.text.Document%29\"相对=nofollow> PdfPageEvent.onEndPage() 来做到这一点。你可以找到样本,通过关键字页面事件及其用法>的OnEndPage ---这些样品从在行动第2版 iText的拍摄。同时要添加背景图形的样品主要是添加页脚和头文件。

Whenever you want to apply something to every page, you should use page events, more specifically PdfPageEvent.onEndPage(), to do it. You can find samples for its usage by keyword Page events > onEndPage --- these samples are taken from iText in Action 2nd Edition. The samples mainly add footers and headers while you want to add background graphics.

请注意,你不应该将内容添加到这里的文件实例,而是直接将 PdfWriter,和你想要的形象在页面下的内容,而不是它上面,你就需要使用 PdfWriter.getDirectContentUnder()像样本的 文具 并没有 PdfWriter? .getDirectContent()像其他样品中

Be aware that you shouldn't add content to the Document instance here but instead directly to the PdfWriter, and as you want the image to be under the page content , not above it, you will need to use PdfWriter.getDirectContentUnder() like in the sample Stationery and not PdfWriter.getDirectContent() like in the other samples.

PS:作为.NET中的类似样本,可以发现这里

PS: The analogous samples for .Net can be found here.

PPS:样品 ImageDirect.java / ImageDirect.cs 显示了如何将图像添加到一些直接内容可能是在这里失去的信息。

PPS: The sample ImageDirect.java / ImageDirect.cs shows how to add an image to some direct content which might be the information missing here.

这篇关于如何图像背景添加到PDF的每一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆