Set a fixed background image for all my pages in PDF iText ASP C #

On the Click button, I create 4 pages in 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 only works with 1 page, but when I create a PDF that contains many entries and has multiple pages, the bg image is only on the last page. I want to apply a background image to all pages.

+3


source to share


1 answer


It is normal that the background is added only once, because you only add it once.

If you want to add content to each page, you don't have to do it manually because you don't know when the new iText page will be created. You should use the page event instead. This is explained in chapter 5 of my book (for the C # version of the examples, see http://tinyurl.com/itextsharpIIA2C05 ).

A good example can be found in the Stationery example in Chapter 6: Stationery.cs

The idea is to create an implementation of the interface PdfPageEvent

, for example by extending the class PdfPageEventHelper

and overriding the method 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'll add PdfTemplate

, but it's very easy to add Image

by replacing an instance with an Stationery

instance Image

and replacing a method AddTemplate()

with a method AddImage()

.

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

:

writer.PageEvent = new TemplateHelper(this);

      



From now on, your method OnEndPage()

will execute every time the page is completed.

Warning: as described in the doc, you shouldn't use a method OnStartPage()

to add content to the page event!

Update:

The end result will look something 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 seem to be quite large. Are you sure this is your page size?

+2


source







All Articles