Header and footer examples
These examples were written in answer to a question on Stack Overflow regarding stamping content on an existing document in iText5. They have been ported to the latest iText version.
stampheader1
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.stamper;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import java.io.File;
public class StampHeader1 {
public static final String DEST = "./target/sandbox/stamper/stamp_header1.pdf";
public static final String SRC = "./src/main/resources/pdfs/Right.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new StampHeader1().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph header = new Paragraph("Copy")
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA))
.setFontSize(14)
.setFontColor(ColorConstants.RED);
for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
Rectangle pageSize = pdfDoc.getPage(i).getPageSize();
float x = pageSize.getWidth() / 2;
float y = pageSize.getTop() - 20;
doc.showTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Stamper
{
public class StampHeader1
{
public static readonly String DEST = "results/sandbox/stamper/stamp_header1.pdf";
public static readonly String SRC = "../../../resources/pdfs/Right.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new StampHeader1().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph header = new Paragraph("Copy")
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
.SetFontSize(14)
.SetFontColor(ColorConstants.RED);
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdfDoc.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
doc.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
doc.Close();
}
}
}
stampheader2
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.stamper;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import java.io.File;
public class StampHeader2 {
public static final String DEST = "./target/sandbox/stamper/stamp_header2.pdf";
public static final String SRC = "./src/main/resources/pdfs/Wrong.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new StampHeader2().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph header = new Paragraph("Copy")
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA))
.setFontSize(14);
for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
Rectangle pageSize = pdfDoc.getPage(i).getPageSize();
float x = pageSize.getWidth() / 2;
float y = pageSize.getTop() - 20;
doc.showTextAligned(header, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
}
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Stamper
{
public class StampHeader2
{
public static readonly String DEST = "results/sandbox/stamper/stamp_header2.pdf";
public static readonly String SRC = "../../../resources/pdfs/Wrong.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new StampHeader2().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph header = new Paragraph("Copy")
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
.SetFontSize(14);
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdfDoc.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
doc.ShowTextAligned(header, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
}
doc.Close();
}
}
}
stampheader3
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.stamper;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import java.io.File;
public class StampHeader3 {
public static final String DEST = "./target/sandbox/stamper/stamp_header3.pdf";
public static final String SRC = "./src/main/resources/pdfs/Wrong.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new StampHeader3().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph header = new Paragraph("Copy")
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA)).setFontSize(6);
for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
PdfPage pdfPage = pdfDoc.getPage(i);
// When "true": in case the page has a rotation, then new content will be automatically rotated in the
// opposite direction. On the rotated page this would look as if new content ignores page rotation.
pdfPage.setIgnorePageRotationForContent(true);
Rectangle pageSize = pdfPage.getPageSize();
float x, y;
if (pdfPage.getRotation() % 180 == 0) {
x = pageSize.getWidth() / 2;
y = pageSize.getTop() - 20;
} else {
x = pageSize.getHeight() / 2;
y = pageSize.getRight() - 20;
}
doc.showTextAligned(header, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
}
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Stamper
{
public class StampHeader3
{
public static readonly String DEST = "results/sandbox/stamper/stamp_header3.pdf";
public static readonly String SRC = "../../../resources/pdfs/Wrong.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new StampHeader3().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph header = new Paragraph("Copy")
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA)).SetFontSize(6);
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
PdfPage pdfPage = pdfDoc.GetPage(i);
// When "true": in case the page has a rotation, then new content will be automatically rotated in the
// opposite direction. On the rotated page this would look as if new content ignores page rotation.
pdfPage.SetIgnorePageRotationForContent(true);
Rectangle pageSize = pdfPage.GetPageSize();
float x;
float y;
if (pdfPage.GetRotation() % 180 == 0)
{
x = pageSize.GetWidth() / 2;
y = pageSize.GetTop() - 20;
}
else
{
x = pageSize.GetHeight() / 2;
y = pageSize.GetRight() - 20;
}
doc.ShowTextAligned(header, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
}
doc.Close();
}
}
}