Search code examples
javaitext7

Using Single Template for all PDF Pages in IText 7


I have the following PDF template which should be consistent for all the pages that gets added in the PDF I am creating ,

enter image description here

The issue is that, am getting this template only for Page 1 and for rest of the pages only blank template is used , Here's the code am using right now,

PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC),new PdfWriter(baosPDF));

PageSize ps = new PageSize(900, 780);

// Initialize document
Document document = new Document(pdfDoc, ps);
document.setMargins(80f, 20f, 50f, 20f);

PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
for(int i = 0; i < 10; i++){
    document.add(new Paragraph("Some Content"));
    document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
document.close();

I have referred this itextsharp add 1 page template to all pages example already , but I need something specific to Itext 7 since it varies a lot from 5.x.x versions

how do I get my PDF to have the single template as shown in image to be common for all the pages ?


Solution

  • As explained in the comments, you need to create an IEventHandler as described in chapter 7 of the tutorial

    This is an example from the PDF to HTML tutorial (chapter 4).

    class Background implements IEventHandler {
        PdfXObject stationery;
    
        public Background(PdfDocument pdf, String src) throws IOException {
            PdfDocument template = new PdfDocument(new PdfReader(src));
            PdfPage page = template.getPage(1);
            stationery = page.copyAsFormXObject(pdf);
            template.close();
        }
    
        @Override
        public void handleEvent(Event event) {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
            PdfDocument pdf = docEvent.getDocument();
            PdfPage page = docEvent.getPage();
            PdfCanvas pdfCanvas = new PdfCanvas(
                page.newContentStreamBefore(), page.getResources(), pdf);
            pdfCanvas.addXObject(stationery, 0, 0);
            Rectangle rect = new Rectangle(36, 32, 36, 64);
            Canvas canvas = new Canvas(pdfCanvas, pdf, rect);
            canvas.add(
                new Paragraph(String.valueOf(pdf.getNumberOfPages()))
                    .setFontSize(48).setFontColor(Color.WHITE));
            canvas.close();
        }
    }
    

    As you can see, we read the template in the constructor, and we draw it to the Canvas in the handleEvent() method. In this example, we also add a page number in white, you can remove all those lines.

    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdf = docEvent.getDocument();
        PdfPage page = docEvent.getPage();
        PdfCanvas pdfCanvas = new PdfCanvas(
            page.newContentStreamBefore(), page.getResources(), pdf);
        pdfCanvas.addXObject(stationery, 0, 0);
    }
    

    Obviously, you also need to declare the handler:

    PdfDocument pdf = new PdfDocument(writer);
    IEventHandler handler = new Background(pdf, stationery);
    pdf.addEventHandler(PdfDocumentEvent.START_PAGE, handler);