Page events for Chunks

Tags: page eventschunk eventgeneric tagcounteriText 7

These examples wer written in answer to questions such as:

Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29260730/how-do-you-underline-text-with-dashedline-in-itext-pdf
 */
package com.itextpdf.samples.sandbox.events;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TextRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class DashedUnderline extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/events/dashed_underline.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DashedUnderline().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        Document doc = new Document(pdfDoc);
        doc.add(new Paragraph("This text is not underlined"));
        Text text1 = new Text("This text is underlined with a solid line");
        text1.setUnderline(1, -3);
        doc.add(new Paragraph(text1));
        Text text2 = new Text("This text is underlined with a dashed line");
        text2.setNextRenderer(new DashedLineTextRenderer(text2));
        doc.add(new Paragraph(text2));
        doc.close();
    }
 
 
    protected class DashedLineTextRenderer extends TextRenderer {
        public DashedLineTextRenderer(Text textElement) {
            super(textElement);
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            Rectangle rect = this.getOccupiedAreaBBox();
            PdfCanvas canvas = drawContext.getCanvas();
            canvas
                    .saveState()
                    .setLineDash(3, 3)
                    .moveTo(rect.getLeft(), rect.getBottom() - 3)
                    .lineTo(rect.getRight(), rect.getBottom() - 3)
                    .stroke()
                    .restoreState();
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/28709603/draw-a-line-every-n-words-using-itextsharp
 */
package com.itextpdf.samples.sandbox.events;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TextRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.*;
 
@Category(SampleTest.class)
public class Every25Words extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/events/every25words.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Every25Words().manipulatePdf(DEST);
    }
 
    public String readFile() throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                new FileInputStream("./src/test/resources/txt/liber1_1_la.txt"), "UTF8"));
        String str;
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        return sb.toString();
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        Document doc = new Document(pdfDoc);
        String[] words = readFile().split("\\s+");
        Paragraph paragraph = new Paragraph();
        Text text = null;
        int i = 0;
        for (String word : words) {
            if (text != null) {
                paragraph.add(" ");
            }
            text = new Text(word);
            text.setNextRenderer(new Word25TextRenderer(text, ++i));
            paragraph.add(text);
        }
        doc.add(paragraph);
        doc.close();
    }
 
 
    private class Word25TextRenderer extends TextRenderer {
        private int count = 0;
 
        public Word25TextRenderer(Text textElement, int count) {
            super(textElement);
            this.count = count;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            if (0 == count % 25) {
                Rectangle rect = getOccupiedAreaBBox();
                int pageNumber = getOccupiedArea().getPageNumber();
                PdfCanvas canvas = drawContext.getCanvas();
                canvas.saveState()
                        .setLineDash(5, 5)
                        .moveTo(drawContext.getDocument().getPage(pageNumber).getPageSize().getLeft(), rect.getBottom())
                        .lineTo(rect.getRight(), rect.getBottom())
                        .lineTo(rect.getRight(), rect.getTop())
                        .lineTo(drawContext.getDocument().getDefaultPageSize().getRight(), rect.getTop())
                        .stroke()
                        .restoreState();
            }
        }
    }
}
File nameRaw URLUpdated
DashedUnderline.javaDashedUnderline.java2016-08-15 10:10 am
Every25Words.javaEvery25Words.java2016-08-15 10:10 am
Resources: 
File nameRaw URLUpdated
liber1_1_la.txtliber1_1_la.txt2016-08-15 10:12 am
Results: 
File nameRaw URLUpdated
cmp_dashed_underline.pdfcmp_dashed_underline.pdf2016-08-15 10:14 am
cmp_every25words.pdfcmp_every25words.pdf2016-08-15 10:14 am