Adding backgrounds using page events
Examples written in answer to questions such as:
pagebackgrounds
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.colors.ColorConstants;
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.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PageBackgrounds {
public static final String DEST = "./target/sandbox/events/page_backgrounds.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new PageBackgrounds().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new PageBackgroundsEventHandler());
Document doc = new Document(pdfDoc);
doc.add(new Paragraph("Prime Numbers"));
doc.add(new AreaBreak());
doc.add(new Paragraph("An overview"));
doc.add(new AreaBreak());
for (int i = 2; i < 301; i++) {
List<Integer> factors = getFactors(i);
if (factors.size() == 1) {
doc.add(new Paragraph("This is a prime number!"));
}
for (int factor : factors) {
doc.add(new Paragraph("Factor: " + factor));
}
if (300 != i) {
doc.add(new AreaBreak());
}
}
doc.close();
}
private static List<Integer> getFactors(int n) {
List<Integer> factors = new ArrayList<>();
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
return factors;
}
private static class PageBackgroundsEventHandler implements IEventHandler {
@Override
public void handleEvent(Event currentEvent) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
PdfPage page = docEvent.getPage();
int pageNumber = docEvent.getDocument().getNumberOfPages();
// Background color will be applied to the first page and all even pages
if (pageNumber % 2 == 1 && pageNumber != 1) {
return;
}
PdfCanvas canvas = new PdfCanvas(page);
Rectangle rect = page.getPageSize();
canvas
.saveState()
.setFillColor(pageNumber < 3 ? ColorConstants.BLUE : ColorConstants.LIGHT_GRAY)
.rectangle(rect)
.fillStroke()
.restoreState();
}
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Events;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Events
{
public class PageBackgrounds
{
public static readonly String DEST = "results/sandbox/events/page_backgrounds.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new PageBackgrounds().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, new PageBackgroundsEventHandler());
Document doc = new Document(pdfDoc);
doc.Add(new Paragraph("Prime Numbers"));
doc.Add(new AreaBreak());
doc.Add(new Paragraph("An overview"));
doc.Add(new AreaBreak());
for (int i = 2; i < 301; i++)
{
List<int> factors = GetFactors(i);
if (factors.Count == 1)
{
doc.Add(new Paragraph("This is a prime number!"));
}
foreach (int factor in factors)
{
doc.Add(new Paragraph("Factor: " + factor));
}
if (300 != i)
{
doc.Add(new AreaBreak());
}
}
doc.Close();
}
private static List<int> GetFactors(int n)
{
List<int> factors = new List<int>();
for (int i = 2; i <= n; i++)
{
while (n % i == 0)
{
factors.Add(i);
n /= i;
}
}
return factors;
}
private class PageBackgroundsEventHandler : IEventHandler
{
public void HandleEvent(Event currentEvent)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
PdfPage page = docEvent.GetPage();
int pageNumber = docEvent.GetDocument().GetNumberOfPages();
// Background color will be applied to the first page and all even pages
if (pageNumber % 2 == 1 && pageNumber != 1)
{
return;
}
PdfCanvas canvas = new PdfCanvas(page);
Rectangle rect = page.GetPageSize();
canvas
.SaveState()
.SetFillColor(pageNumber < 3 ? ColorConstants.BLUE : ColorConstants.LIGHT_GRAY)
.Rectangle(rect)
.FillStroke()
.RestoreState();
}
}
}
}
pageborder
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.colors.ColorConstants;
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.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PageBorder {
public static final String DEST = "./target/sandbox/events/page_border.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new PageBorder().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new PageBorderEventHandler());
Document doc = new Document(pdfDoc);
for (int i = 2; i < 301; i++) {
List<Integer> factors = getFactors(i);
if (factors.size() == 1) {
doc.add(new Paragraph("This is a prime number!"));
}
for (int factor : factors) {
doc.add(new Paragraph("Factor: " + factor));
}
if (300 != i) {
doc.add(new AreaBreak());
}
}
doc.close();
}
private static List<Integer> getFactors(int n) {
List<Integer> factors = new ArrayList<>();
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
return factors;
}
private static class PageBorderEventHandler implements IEventHandler {
@Override
public void handleEvent(Event currentEvent) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
PdfCanvas canvas = new PdfCanvas(docEvent.getPage());
Rectangle rect = docEvent.getPage().getPageSize();
canvas
.setLineWidth(5)
.setStrokeColor(ColorConstants.RED)
.rectangle(rect)
.stroke();
}
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Events;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Events
{
public class PageBorder
{
public static readonly String DEST = "results/sandbox/events/page_border.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new PageBorder().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, new PageBorderEventHandler());
Document doc = new Document(pdfDoc);
for (int i = 2; i < 301; i++)
{
IList<int> factors = GetFactors(i);
if (factors.Count == 1)
{
doc.Add(new Paragraph("This is a prime number!"));
}
foreach (int factor in factors)
{
doc.Add(new Paragraph("Factor: " + factor));
}
if (300 != i)
{
doc.Add(new AreaBreak());
}
}
doc.Close();
}
private static IList<int> GetFactors(int n)
{
IList<int> factors = new List<int>();
for (int i = 2; i <= n; i++)
{
while (n % i == 0)
{
factors.Add(i);
n /= i;
}
}
return factors;
}
private class PageBorderEventHandler : IEventHandler
{
public void HandleEvent(Event currentEvent)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
PdfCanvas canvas = new PdfCanvas(docEvent.GetPage());
Rectangle rect = docEvent.GetPage().GetPageSize();
canvas
.SetLineWidth(5)
.SetStrokeColor(ColorConstants.RED)
.Rectangle(rect)
.Stroke();
}
}
}
}