在PDF iText ASP C#中为我的所有页面设置修复背景图像 [英] Set a fix background image for all my pages in PDF iText ASP C#
问题描述
开启按钮点击,我在我的PDF上生成4个页面,我添加了此图像以提供背景图像
On Button Click, I generate 4 pages on my PDF, i added this image to provide a background image
string imageFilePath = parent + "/Images/bg_image.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(1700, 1000);
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
jpg.SetAbsolutePosition(0, 0);
document.Add(jpg);
它仅适用于1页,但是当我生成包含许多记录并且有多个页面的PDF时,bg图像仅在最后一页。我想将背景图像应用到所有页面。
It works only with 1 page, but when i generate a PDF that contains many records and have several pages, the bg image is only at the last page. I want to apply the background image to all of the pages.
推荐答案
背景只添加一次是正常的,因为你只需要添加一次。
It is normal that the background is added only once, because you're adding it only once.
如果你想为每个页面添加内容,你不应该手动手动,因为你不要知道iText何时会创建新页面。相反,您应该使用页面事件。 本书第5章对此进行了解释(对于C#版本的示例) ,请参阅 http://tinyurl.com/itextsharpIIA2C05 。)
If you want to add content to every page, you should not do that manually because you don't know when a new page will be created by iText. Instead you should use a page event. This is explained in Chapter 5 of my book (for the C# version of the examples, see http://tinyurl.com/itextsharpIIA2C05 ).
在第6章的文具示例中可以找到一个很好的示例: Stationery.cs
A nice example can be found in the Stationery example in chapter 6: Stationery.cs
想法是创建 PdfPageEvent
接口的实现,例如扩展 PdfPageEventHelper
class并覆盖 OnEndPage()
方法:
The idea is to create an implementation of the PdfPageEvent
interface, for instance by extending the PdfPageEventHelper
class and overriding the OnEndPage()
method:
class TemplateHelper : PdfPageEventHelper {
private Stationery instance;
public TemplateHelper() { }
public TemplateHelper(Stationery instance) {
this.instance = instance;
}
/**
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public override void OnEndPage(PdfWriter writer, Document document) {
writer.DirectContentUnder.AddTemplate(instance.page, 0, 0);
}
}
在这种情况下,我们添加 PdfTemplate
,但很容易添加 Image
替换文具
实例使用 Image
实例并使用 AddImage()替换
方法。 AddTemplate()
方法
In this case, we add a PdfTemplate
, but it is very easy to add an Image
replacing the Stationery
instance with an Image
instance and replacing the AddTemplate()
method with the AddImage()
method.
一旦你有自定义页面事件的实例,你需要将它声明为 PdfWriter
instance:
Once you have an instance of your custom page event, you need to declare it to the PdfWriter
instance:
writer.PageEvent = new TemplateHelper(this);
从那一刻开始,你的 OnEndPage()
方法。
From that moment on, your OnEndPage()
method will be executed each time a page is finalized.
警告:如您所记录的不得使用 OnStartPage()
在页面事件中添加内容的方法!
Warning: as documented you shall not use the OnStartPage()
method to add content in a page event!
更新:
最终结果看起来或多或少如下:
The final result would look more or less like this:
class ImageBackgroundHelper : PdfPageEventHelper {
private Image img;
public ImageBackgroundHelper(Image img) {
this.img = img;
}
/**
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public override void OnEndPage(PdfWriter writer, Document document) {
writer.DirectContentUnder.AddImage(img);
}
}
现在你可以像这样使用这个事件:
Now you can use this event like this:
string imageFilePath = parent + "/Images/bg_image.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(1700, 1000);
jpg.SetAbsolutePosition(0, 0);
writer.PageEvent = new ImageBackgroundHelper(jpg);
请注意,1700和1000似乎相当大。您确定这些是您网页的尺寸吗?
Note that 1700 and 1000 seems quite big. Are you sure those are the dimensions of your page?
这篇关于在PDF iText ASP C#中为我的所有页面设置修复背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!