Page events for Chunks

Tags: page eventschunk eventgeneric tagcounteriText 5

These examples wer written in answer to questions such as:

Files: 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29260730/how-do-you-underline-text-with-dashedline-in-itext-pdf
 */
package sandbox.events;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class DashedUnderline {
 
    public static final String DEST = "results/events/dashed_underline.pdf";
 
    public class DashedLine extends PdfPageEventHelper {
 
        @Override
        public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
            PdfContentByte canvas = writer.getDirectContent();
            canvas.saveState();
            canvas.setLineDash(3, 3);
            canvas.moveTo(rect.getLeft(), rect.getBottom() - 3);
            canvas.lineTo(rect.getRight(), rect.getBottom() - 3);
            canvas.stroke();
            canvas.restoreState();
        }
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DashedUnderline().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        writer.setPageEvent(new DashedLine());
        document.open();
        document.add(new Paragraph("This text is not underlined"));
        Chunk chunk1 = new Chunk("This text is underlined with a solid line");
        chunk1.setUnderline(1, -3);
        document.add(new Paragraph(chunk1));
        Chunk chunk2 = new Chunk("This text is underlined with a dashed line");
        chunk2.setGenericTag("");
        document.add(new Paragraph(chunk2));
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/28709603/draw-a-line-every-n-words-using-itextsharp
 */
package sandbox.events;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import sandbox.WrapToTest;
 
/**
 *
 * @author iText
 */
@WrapToTest
public class Every25Words {
 
    public static final String DEST = "results/events/every25words.pdf";
 
    public class WordCounter extends PdfPageEventHelper {
 
        public int count = 0;
 
        @Override
        public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
            count++;
            if (count % 25 == 0) {
                PdfContentByte canvas = writer.getDirectContent();
                canvas.saveState();
                canvas.setLineDash(5, 5);
                canvas.moveTo(document.left(), rect.getBottom());
                canvas.lineTo(rect.getRight(), rect.getBottom());
                canvas.lineTo(rect.getRight(), rect.getTop());
                canvas.lineTo(document.right(), rect.getTop());
                canvas.stroke();
                canvas.restoreState();
            }
        }
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Every25Words().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        writer.setPageEvent(new WordCounter());
        writer.setInitialLeading(16);
        document.open();
        String[] words = readFile().split("\\s+");
        Chunk chunk = null;
        for (String word : words) {
            if (chunk != null) {
                document.add(new Chunk(" "));
            }
            chunk = new Chunk(word);
            chunk.setGenericTag("");
            document.add(chunk);
        }
        document.close();
    }
 
    public String readFile() throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream("resources/text/liber1_1_la.txt"), "UTF8"));
        String str;
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        return sb.toString();
    }
}
package sandbox.events;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.events.IndexEvents;
import com.itextpdf.text.pdf.events.IndexEvents.Entry;
 
public class IndexWords {
 
    public static final String DEST = "results/events/index_words.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new IndexWords().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        IndexEvents index = new IndexEvents();
        writer.setPageEvent(index);
        document.open();
		Paragraph p = new Paragraph("Quick brown fox ");
		p.add(index.create("jumps", "Jump"));
		p.add(" over the lazy dog.");
		document.add(p);
		document.newPage();
 
		p = new Paragraph();
		p.add(index.create("Quick brown fox", "Fox", "quick, brown"));
		p.add(new Chunk(" jumps over "));
		p.add(index.create("the lazy dog.", "Dog", "lazy"));
		p.add(index.create(" ", "Jumping"));
		document.add(p);
		document.newPage();
 
		p = new Paragraph();
		p.add(new Chunk("The fox is "));
		p.add(index.create("brown", "Color", "brown"));
		p.add(index.create(" ", "Brown", "color", "see Color; brown"));
		p.add(Chunk.NEWLINE);
		document.add(p);
		document.newPage();
 
		p = new Paragraph();
		p.add(new Chunk("The dog is "));
		p.add(index.create("yellow", "Color", "yellow"));
		p.add(index.create(" ", "Yellow", "color", "see Color; yellow"));
		p.add(Chunk.NEWLINE);
		document.add(p);
		document.newPage();
 
		// we add the index
		document.add(new Paragraph("Index:"));
		List<Entry> list = index.getSortedEntries();
		for (Entry entry : list) {
			Paragraph in = new Paragraph();
			in.add(new Chunk(entry.getIn1()));
			if (entry.getIn2().length() > 0) {
				in.add(new Chunk("; " + entry.getIn2()));
			}
			if (entry.getIn3().length() > 0) {
				in.add(new Chunk(" (" + entry.getIn3() + ")"));
			}
			List<Integer> pages = entry.getPagenumbers();
			List<String> tags = entry.getTags();
			in.add(": ");
			for (int i = 0, x = pages.size(); i < x; i++) {
				Chunk pagenr = new Chunk(" p" + pages.get(i));
				pagenr.setLocalGoto((String) tags.get(i));
				in.add(pagenr);
			}
			document.add(in);
		}
 
		document.close();
	}
}
File nameRaw URLUpdated
DashedUnderline.javaDashedUnderline.java2015-11-08 1:27 pm
Every25Words.javaEvery25Words.java2015-10-24 1:26 am
IndexWords.javaIndexWords.java2016-09-26 4:49 pm
Resources: 
File nameRaw URLUpdated
liber1_1_la.txtliber1_1_la.txt2015-10-24 1:26 am
Results: