Page events for Chunks
These examples were written in answer to questions such as:
dashedunderline
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.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.IRenderer;
import com.itextpdf.layout.renderer.TextRenderer;
import java.io.File;
public class DashedUnderline {
public static final String DEST = "./target/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);
}
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();
}
private static class DashedLineTextRenderer extends TextRenderer {
public DashedLineTextRenderer(Text textElement) {
super(textElement);
}
// If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
// If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
// renderer will be created
@Override
public IRenderer getNextRenderer() {
return new DashedLineTextRenderer((Text) modelElement);
}
@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();
}
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Renderer;
namespace iText.Samples.Sandbox.Events
{
public class DashedUnderline
{
public static readonly String DEST = "results/sandbox/events/dashed_underline.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new DashedUnderline().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
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();
}
private class DashedLineTextRenderer : TextRenderer
{
public DashedLineTextRenderer(Text textElement) : base(textElement)
{
}
// If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
// If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
// renderer will be created
public override IRenderer GetNextRenderer()
{
return new DashedLineTextRenderer((Text) modelElement);
}
public override void Draw(DrawContext drawContext)
{
base.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();
}
}
}
}
every25words
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.events;
import com.itextpdf.kernel.geom.PageSize;
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.IRenderer;
import com.itextpdf.layout.renderer.TextRenderer;
import java.io.*;
public class Every25Words {
public static final String DEST = "./target/sandbox/events/every25words.pdf";
public static final String SRC = "./src/main/resources/txt/liber1_1_la.txt";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Every25Words().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
String[] words = readFile(SRC).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 static String readFile(String filePath) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF8"))) {
String str;
while ((str = reader.readLine()) != null) {
sb.append(str);
}
}
return sb.toString();
}
private static class Word25TextRenderer extends TextRenderer {
private int count = 0;
public Word25TextRenderer(Text textElement, int count) {
super(textElement);
this.count = count;
}
// If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
// If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
// renderer will be created
@Override
public IRenderer getNextRenderer() {
return new Word25TextRenderer((Text) modelElement, count);
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
// Draws a line to delimit the text every 25 words
if (0 == count % 25) {
Rectangle textRect = getOccupiedAreaBBox();
int pageNumber = getOccupiedArea().getPageNumber();
PdfCanvas canvas = drawContext.getCanvas();
Rectangle pageRect = drawContext.getDocument().getPage(pageNumber).getPageSize();
canvas
.saveState()
.setLineDash(5, 5)
.moveTo(pageRect.getLeft(), textRect.getBottom())
.lineTo(textRect.getRight(), textRect.getBottom())
.lineTo(textRect.getRight(), textRect.getTop())
.lineTo(pageRect.getRight(), textRect.getTop())
.stroke()
.restoreState();
}
}
}
}
C#
C#
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Renderer;
namespace iText.Samples.Sandbox.Events
{
public class Every25Words
{
public static readonly String DEST = "results/sandbox/events/every25words.pdf";
public static readonly String SRC = "../../../resources/text/liber1_1_la.txt";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new Every25Words().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
String file = ReadFile(SRC);
String[] words = Regex.Split(file, "\\s+");
Paragraph paragraph = new Paragraph();
Text text = null;
int i = 0;
foreach (String word in 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 static String ReadFile(String filePath)
{
StringBuilder sb = new StringBuilder();
using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open), Encoding.UTF8))
{
String str;
while ((str = reader.ReadLine()) != null)
{
sb.Append(str);
}
}
return sb.ToString();
}
private class Word25TextRenderer : TextRenderer
{
private int count = 0;
public Word25TextRenderer(Text textElement, int count) : base(textElement)
{
this.count = count;
}
// If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
// If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
// renderer will be created
public override IRenderer GetNextRenderer()
{
return new Word25TextRenderer((Text) modelElement, count);
}
public override void Draw(DrawContext drawContext)
{
base.Draw(drawContext);
// Draws a line to delimit the text every 25 words
if (0 == count % 25)
{
Rectangle textRect = GetOccupiedAreaBBox();
int pageNumber = GetOccupiedArea().GetPageNumber();
PdfCanvas canvas = drawContext.GetCanvas();
Rectangle pageRect = drawContext.GetDocument().GetPage(pageNumber).GetPageSize();
canvas
.SaveState()
.SetLineDash(5, 5)
.MoveTo(pageRect.GetLeft(), textRect.GetBottom())
.LineTo(textRect.GetRight(), textRect.GetBottom())
.LineTo(textRect.GetRight(), textRect.GetTop())
.LineTo(pageRect.GetRight(), textRect.GetTop())
.Stroke()
.RestoreState();
}
}
}
}
}