3

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.

dandy
  • 67
  • 5
  • 13

1 Answers1

1

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

A nice example can be found in the Stationery example in chapter 6: Stationery.cs

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!

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?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    i keep on having error that says "ImageBackgroundHelper does not contain a constructor that takes 1 argument" for the code writer.PageEvent = new ImageBackgroundHelper(jpg); – dandy Nov 01 '14 at 12:06
  • 2
    I can clearly see the constructor: `public ImageBackgroundHelper(Image img) { this.img = img; }` Please don't troll StackOverflow! – Bruno Lowagie Nov 01 '14 at 12:33