PdfPageEventHelperDemo.java Source code

Java tutorial

Introduction

Here is the source code for PdfPageEventHelperDemo.java

Source

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

public class PdfPageEventHelperDemo extends PdfPageEventHelper {
    public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
        if ("text".equals(text)) {
            PdfContentByte cb = writer.getDirectContent();
            cb.ellipse(rect.left(), rect.bottom() - 5f, rect.right(), rect.top());
            cb.stroke();
        }
    }

    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PdfPageEventHelperDemo.pdf"));
            writer.setPageEvent(new PdfPageEventHelperDemo());
            document.open();

            Paragraph p = new Paragraph("Generic page event");
            document.add(p);

            Chunk box = new Chunk("text");
            box.setGenericTag("text");

            p.add(box);
            document.add(p);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }
}