How can I add an image to all pages of my PDF?

Solution 1:

It's normal that the image is only added once; after all: you're adding it only once. (Or you've left away some essential steps in your code snippet: see the edit I made.)

In any case: you can solve your problem by using a page event. There are some examples in Java here: http://itextpdf.com/sandbox/events

Or you can consult chapter 5 of my book. All examples are available in Java as well as in C#.

You should create a document in 5 steps and add an event in step 2:

// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, stream);
MyEvent event = new MyEvent();
writer.PageEvent = event;
// step 3
document.Open();
// step 4
// Add whatever content you want to add
// step 5
document.Close();

You have to write the MyEvent class yourself:

protected class MyEvent : PdfPageEventHelper {

    Image image;

    public override void OnOpenDocument(PdfWriter writer, Document document) {
        image = Image.GetInstance(Server.MapPath("~/images/draft.png"));
        image.SetAbsolutePosition(12, 300);
    }

    public override void OnEndPage(PdfWriter writer, Document document) {
        writer.DirectContent.AddImage(image);
    }
}

The OnEndPage() in class MyEvent will be triggered every time the PdfWriter has finished a page. Hence the image will be added on every page.

Caveat: it is important to create the image object outside the OnEndPage() method, otherwise the image bytes risk being added as many times as there are pages in your PDF (leading to a bloated PDF).

Solution 2:

Document document = new Document();
// step 2

PdfWriter writer = PdfWriter.GetInstance(document, stream);

 final MyEvent event = new MyEvent();

    writer.setPageEvent(event);

    document.Open();

// Add whatever content you want to add

   document.Close();

//Now Create The new class and override the onEndPage()
//I have used this for footer image but you can add wherever you want 
//in the whole page using setAbsolutePosition(see below the class)
//I am using palyFramework with java
 package controllers;

import java.io.IOException;
import java.nio.file.Files;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import play.Play;

public class MyEvent extends PdfPageEventHelper{

@Override
public void onEndPage(final PdfWriter writer, final Document document) 
{
    final Font ffont = new Font(Font.FontFamily.TIMES_ROMAN,8, 
Font.ITALIC);
    Image img;
    try {
        final Phrase footer = new Phrase("Powered By :", ffont);
img=Image.getInstance(Files.readAllBytes(Play.application().getFile("/
                                     img- path/Image.png").toPath()));
        img.scaleToFit(60f, 40f);
           img.setAbsolutePosition(510,5);
        final PdfContentByte cb = writer.getDirectContent();
        cb.addImage(img);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
                footer,490, 15, 0);
    } catch (IOException | DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}

It works fine for me; refer to this link.