How to set a fixed background image for all my pages?

Category: 
Tags: page eventsbackgroundimagesiText 5

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

Posted on StackOverflow on Nov 1, 2014 by dandy

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.

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

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.

Once you have an instance of your custom page event, you need to declare it to the PdfWriter instance:

writer.PageEvent = new TemplateHelper(this);

From that moment on, your OnEndPage() method will be executed each time a page is finalized.

Warning: as documented you shall not use the OnStartPage() method to add content in a page event!

If we adapt the above example to your requirement, 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?