pdfHTML: Header and Footer examples
pdfhtmlheaderandfooter
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.pdfhtml;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PdfHtmlHeaderAndFooter {
public static final String SRC = "./src/main/resources/pdfhtml/";
public static final String DEST = "./target/sandbox/pdfhtml/ipsum.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
String htmlSource = SRC + "ipsum.html";
new PdfHtmlHeaderAndFooter().manipulatePdf(htmlSource, DEST);
}
public void manipulatePdf(String htmlSource, String pdfDest) throws IOException {
PdfWriter writer = new PdfWriter(pdfDest);
PdfDocument pdfDocument = new PdfDocument(writer);
String header = "pdfHtml Header and footer example using page-events";
Header headerHandler = new Header(header);
Footer footerHandler = new Footer();
pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
// Base URI is required to resolve the path to source files
ConverterProperties converterProperties = new ConverterProperties().setBaseUri(SRC);
HtmlConverter.convertToDocument(new FileInputStream(htmlSource), pdfDocument, converterProperties);
// Write the total number of pages to the placeholder
footerHandler.writeTotal(pdfDocument);
pdfDocument.close();
}
// Header event handler
protected class Header implements IEventHandler {
private String header;
public Header(String header) {
this.header = header;
}
@Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
Rectangle pageSize = page.getPageSize();
Canvas canvas = new Canvas(new PdfCanvas(page), pageSize);
canvas.setFontSize(18);
// Write text at position
canvas.showTextAligned(header,
pageSize.getWidth() / 2,
pageSize.getTop() - 30, TextAlignment.CENTER);
canvas.close();
}
}
// Footer event handler
protected class Footer implements IEventHandler {
protected PdfFormXObject placeholder;
protected float side = 20;
protected float x = 300;
protected float y = 25;
protected float space = 4.5f;
protected float descent = 3;
public Footer() {
placeholder = new PdfFormXObject(new Rectangle(0, 0, side, side));
}
@Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
int pageNumber = pdf.getPageNumber(page);
Rectangle pageSize = page.getPageSize();
// Creates drawing canvas
PdfCanvas pdfCanvas = new PdfCanvas(page);
Canvas canvas = new Canvas(pdfCanvas, pageSize);
Paragraph p = new Paragraph()
.add("Page ")
.add(String.valueOf(pageNumber))
.add(" of");
canvas.showTextAligned(p, x, y, TextAlignment.RIGHT);
canvas.close();
// Create placeholder object to write number of pages
pdfCanvas.addXObjectAt(placeholder, x + space, y - descent);
pdfCanvas.release();
}
public void writeTotal(PdfDocument pdf) {
Canvas canvas = new Canvas(placeholder, pdf);
canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
0, descent, TextAlignment.LEFT);
canvas.close();
}
}
}
C#
C#
using System;
using System.IO;
using iText.Html2pdf;
using iText.Kernel.Events;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Pdfhtml
{
public class PdfHtmlHeaderAndFooter
{
public static readonly string SRC = "../../../resources/pdfhtml/";
public static readonly string DEST = "results/sandbox/pdfhtml/ipsum.pdf";
public static void Main(string[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
string htmlSource = SRC + "ipsum.html";
new PdfHtmlHeaderAndFooter().ManipulatePdf(htmlSource, DEST);
}
public void ManipulatePdf(string htmlSource, string pdfDest)
{
PdfWriter writer = new PdfWriter(pdfDest);
PdfDocument pdfDocument = new PdfDocument(writer);
string header = "pdfHtml Header and footer example using page-events";
Header headerHandler = new Header(header);
Footer footerHandler = new Footer();
pdfDocument.AddEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
pdfDocument.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
// Base URI is required to resolve the path to source files
ConverterProperties converterProperties = new ConverterProperties().SetBaseUri(SRC);
HtmlConverter.ConvertToDocument(new FileStream(htmlSource, FileMode.Open), pdfDocument, converterProperties);
// Write the total number of pages to the placeholder
footerHandler.WriteTotal(pdfDocument);
pdfDocument.Close();
}
// Header event handler
protected class Header : IEventHandler
{
private string header;
public Header(string header)
{
this.header = header;
}
public virtual void HandleEvent(Event @event)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent) @event;
PdfDocument pdf = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
Rectangle pageSize = page.GetPageSize();
Canvas canvas = new Canvas(new PdfCanvas(page), pageSize);
canvas.SetFontSize(18);
// Write text at position
canvas.ShowTextAligned(header,
pageSize.GetWidth() / 2,
pageSize.GetTop() - 30, TextAlignment.CENTER);
canvas.Close();
}
}
// Footer event handler
protected class Footer : IEventHandler
{
protected PdfFormXObject placeholder;
protected float side = 20;
protected float x = 300;
protected float y = 25;
protected float space = 4.5f;
protected float descent = 3;
public Footer()
{
placeholder = new PdfFormXObject(new Rectangle(0, 0, side, side));
}
public virtual void HandleEvent(Event @event)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent) @event;
PdfDocument pdf = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
int pageNumber = pdf.GetPageNumber(page);
Rectangle pageSize = page.GetPageSize();
// Creates drawing canvas
PdfCanvas pdfCanvas = new PdfCanvas(page);
Canvas canvas = new Canvas(pdfCanvas, pageSize);
Paragraph p = new Paragraph()
.Add("Page ")
.Add(pageNumber.ToString())
.Add(" of");
canvas.ShowTextAligned(p, x, y, TextAlignment.RIGHT);
canvas.Close();
// Create placeholder object to write number of pages
pdfCanvas.AddXObjectAt(placeholder, x + space, y - descent);
pdfCanvas.Release();
}
public void WriteTotal(PdfDocument pdf)
{
Canvas canvas = new Canvas(placeholder, pdf);
canvas.ShowTextAligned(pdf.GetNumberOfPages().ToString(),
0, descent, TextAlignment.LEFT);
canvas.Close();
}
}
}
}