On Button Click, I generate 4 pages on my PDF, i added this image to provide a background image
在按钮点击,我在我的PDF生成4页,我添加这个图像提供背景图像。
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);
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.
它只适用于1页,但是当我生成一个包含许多记录和若干页的PDF时,bg映像仅在最后一页。我想把背景图像应用到所有的页面。
1 个解决方案
#1
2
It is normal that the background is added only once, because you're adding it only once.
通常只添加一次背景,因为只添加一次背景。
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 ).
如果希望向每个页面添加内容,不应该手动添加,因为不知道iText何时创建新页面。相反,您应该使用页面事件。我的书第5章对此进行了解释(关于c#版本的示例,请参见http://tinyurl.com/itextsharpIIA2C05)。
A nice example can be found in the Stationery example in chapter 6: Stationery.cs
在第6章:stationary .cs中可以找到一个很好的例子
The idea is to create an implementation of the PdfPageEvent
interface, for instance by extending the PdfPageEventHelper
class and overriding the OnEndPage()
method:
其想法是创建PdfPageEvent接口的实现,例如扩展PdfPageEventHelper类并覆盖OnEndPage()方法:
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);
}
}
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.
在本例中,我们添加了一个PdfTemplate,但是很容易添加一个图像,用一个图像实例替换文具实例,并用AddImage()方法替换AddTemplate()方法。
Once you have an instance of your custom page event, you need to declare it to the PdfWriter
instance:
一旦您有了自定义页面事件的实例,您需要将它声明给PdfWriter实例:
writer.PageEvent = new TemplateHelper(this);
From that moment on, your OnEndPage()
method will be executed each time a page is finalized.
从那时起,每完成一个页面,就执行一次OnEndPage()方法。
Warning: as documented you shall not use the OnStartPage()
method to add content in a page event!
警告:如文件所示,您不应使用OnStartPage()方法在页面事件中添加内容!
Update:
更新:
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);
Note that 1700 and 1000 seems quite big. Are you sure those are the dimensions of your page?
注意1700和1000看起来相当大。你确定这些是你页面的尺寸吗?
#1
2
It is normal that the background is added only once, because you're adding it only once.
通常只添加一次背景,因为只添加一次背景。
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 ).
如果希望向每个页面添加内容,不应该手动添加,因为不知道iText何时创建新页面。相反,您应该使用页面事件。我的书第5章对此进行了解释(关于c#版本的示例,请参见http://tinyurl.com/itextsharpIIA2C05)。
A nice example can be found in the Stationery example in chapter 6: Stationery.cs
在第6章:stationary .cs中可以找到一个很好的例子
The idea is to create an implementation of the PdfPageEvent
interface, for instance by extending the PdfPageEventHelper
class and overriding the OnEndPage()
method:
其想法是创建PdfPageEvent接口的实现,例如扩展PdfPageEventHelper类并覆盖OnEndPage()方法:
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);
}
}
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.
在本例中,我们添加了一个PdfTemplate,但是很容易添加一个图像,用一个图像实例替换文具实例,并用AddImage()方法替换AddTemplate()方法。
Once you have an instance of your custom page event, you need to declare it to the PdfWriter
instance:
一旦您有了自定义页面事件的实例,您需要将它声明给PdfWriter实例:
writer.PageEvent = new TemplateHelper(this);
From that moment on, your OnEndPage()
method will be executed each time a page is finalized.
从那时起,每完成一个页面,就执行一次OnEndPage()方法。
Warning: as documented you shall not use the OnStartPage()
method to add content in a page event!
警告:如文件所示,您不应使用OnStartPage()方法在页面事件中添加内容!
Update:
更新:
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);
Note that 1700 and 1000 seems quite big. Are you sure those are the dimensions of your page?
注意1700和1000看起来相当大。你确定这些是你页面的尺寸吗?