pdfHTML: Header and Footer example

This is a code example of iText PDF, discover more.

27th March 2017
iText PDF

Switch code for this example

pdfHTMLHeaderAndFooterExample.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.pdfHTML.HeaderFooter;
  10.  
  11. import com.itextpdf.html2pdf.ConverterProperties;
  12. import com.itextpdf.html2pdf.HtmlConverter;
  13. import com.itextpdf.kernel.events.Event;
  14. import com.itextpdf.kernel.events.IEventHandler;
  15. import com.itextpdf.kernel.events.PdfDocumentEvent;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfPage;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  21. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  22. import com.itextpdf.layout.Canvas;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.property.TextAlignment;
  25. import com.itextpdf.licensekey.LicenseKey;
  26.  
  27. import java.io.File;
  28. import java.io.FileInputStream;
  29. import java.io.IOException;
  30.  
  31. public class pdfHTMLHeaderAndFooterExample {
  32.  
  33.     public static final String sourceFolder = "src/test/resources/pdfHTML/";
  34.     public static final String destinationFolder = "target/output/pdfHTML/";
  35.     public static final String LICENSE = "src/test/resources/pdfHTML/itextkey_trial.xml";
  36.  
  37.     public static final String[] files = {"ipsum"};
  38.  
  39.     public static void main(String[] args) throws IOException, InterruptedException {
  40.         LicenseKey.loadLicenseFile(LICENSE);
  41.         for (String name : files) {
  42.             String htmlSource = sourceFolder + name + ".html";
  43.             String resourceFolder = sourceFolder + name + "/";
  44.             String pdfDest = destinationFolder + name + ".pdf";
  45.             File file = new File(pdfDest);
  46.  
  47.             System.out.println("Parsing: " + htmlSource);
  48.             file.getParentFile().mkdirs();
  49.             new pdfHTMLHeaderAndFooterExample().parseWithHeaderAndFooter(htmlSource, pdfDest, resourceFolder);
  50.  
  51.         }
  52.     }
  53.  
  54.     public void parseWithHeaderAndFooter(String htmlSource, String pdfDest, String resoureLoc) throws IOException, InterruptedException {
  55.         File pdf = new File(pdfDest);
  56.         pdf.getParentFile().mkdirs();
  57.  
  58.         //Create Document
  59.         PdfWriter writer= new PdfWriter(pdfDest);
  60.         PdfDocument pdfDocument = new PdfDocument(writer);
  61.         //Create event-handlers
  62.         String header = "pdfHtml Header and footer example using page-events";
  63.         Header headerHandler = new Header(header);
  64.         PageXofY footerHandler = new PageXofY(pdfDocument);
  65.  
  66.         //Assign event-handlers
  67.         pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE,headerHandler);
  68.         pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE,footerHandler);
  69.  
  70.         //Convert
  71.         ConverterProperties converterProperties = new ConverterProperties().setBaseUri(resoureLoc);
  72.         HtmlConverter.convertToDocument(new FileInputStream(htmlSource), pdfDocument, converterProperties);
  73.         //Write the total number of pages to the placeholder
  74.         footerHandler.writeTotal(pdfDocument);
  75.         pdfDocument.close();
  76.  
  77.  
  78.     }
  79.  
  80.     //Header event handler
  81.     protected class Header implements IEventHandler {
  82.         String header;
  83.         public Header(String header) {
  84.             this.header = header;
  85.         }
  86.         @Override
  87.         public void handleEvent(Event event) {
  88.             //Retrieve document and
  89.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  90.             PdfDocument pdf = docEvent.getDocument();
  91.             PdfPage page = docEvent.getPage();
  92.             Rectangle pageSize = page.getPageSize();
  93.             PdfCanvas pdfCanvas = new PdfCanvas(
  94.                     page.getLastContentStream(), page.getResources(), pdf);
  95.             Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
  96.             canvas.setFontSize(18f);
  97.             //Write text at position
  98.             canvas.showTextAligned(header,
  99.                     pageSize.getWidth() / 2,
  100.                     pageSize.getTop() - 30, TextAlignment.CENTER);
  101.         }
  102.     }
  103.  
  104.     //page X of Y
  105.     protected class PageXofY implements IEventHandler {
  106.         protected PdfFormXObject placeholder;
  107.         protected float side = 20;
  108.         protected float x = 300;
  109.         protected float y = 25;
  110.         protected float space = 4.5f;
  111.         protected float descent = 3;
  112.         public PageXofY(PdfDocument pdf) {
  113.             placeholder =
  114.                     new PdfFormXObject(new Rectangle(0, 0, side, side));
  115.         }
  116.         @Override
  117.         public void handleEvent(Event event) {
  118.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  119.             PdfDocument pdf = docEvent.getDocument();
  120.             PdfPage page = docEvent.getPage();
  121.             int pageNumber = pdf.getPageNumber(page);
  122.             Rectangle pageSize = page.getPageSize();
  123.             PdfCanvas pdfCanvas = new PdfCanvas(
  124.                     page.getLastContentStream(), page.getResources(), pdf);
  125.             Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
  126.             Paragraph p = new Paragraph()
  127.                     .add("Page ").add(String.valueOf(pageNumber)).add(" of");
  128.             canvas.showTextAligned(p, x, y, TextAlignment.RIGHT);
  129.             pdfCanvas.addXObject(placeholder, x + space, y - descent);
  130.             pdfCanvas.release();
  131.         }
  132.         public void writeTotal(PdfDocument pdf) {
  133.             Canvas canvas = new Canvas(placeholder, pdf);
  134.             canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
  135.                     0, descent, TextAlignment.LEFT);
  136.         }
  137.     }
  138.  
  139.  
  140.  
  141. }

Resources

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now