Repeating parts of a form
These examples were written in answer to questions such as:
addextratable
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.acroforms;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
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.Table;
import com.itextpdf.layout.layout.LayoutArea;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.DocumentRenderer;
import com.itextpdf.layout.renderer.IRenderer;
import java.io.File;
import java.util.Map;
public class AddExtraTable {
public static String DEST = "./target/sandbox/acroforms/add_extra_table.pdf";
public static String SRC = "./src/main/resources/pdfs/form.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddExtraTable().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getAllFormFields();
fields.get("Name").setValue("Jeniffer");
fields.get("Company").setValue("iText's next customer");
fields.get("Country").setValue("No Man's Land");
form.flattenFields();
Table table = new Table(UnitValue.createPercentArray(new float[] {1, 15}));
table.setWidth(UnitValue.createPercentValue(80));
table.addHeaderCell("#");
table.addHeaderCell("description");
for (int i = 1; i <= 150; i++) {
table.addCell(String.valueOf(i));
table.addCell("test " + i);
}
// The custom renderer decreases the first page's area.
// As a result, there is not overlapping between the table from acroform and the new one.
doc.setRenderer(new ExtraTableRenderer(doc));
doc.add(table);
doc.close();
}
protected static class ExtraTableRenderer extends DocumentRenderer {
public ExtraTableRenderer(Document document) {
super(document);
}
// 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 ExtraTableRenderer(document);
}
@Override
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
LayoutArea area = super.updateCurrentArea(overflowResult);
if (area.getPageNumber() == 1) {
area.getBBox().decreaseHeight(266);
}
return area;
}
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Layout;
using iText.Layout.Properties;
using iText.Layout.Renderer;
namespace iText.Samples.Sandbox.Acroforms
{
public class AddExtraTable
{
public static readonly String DEST = "results/sandbox/acroforms/add_extra_table.pdf";
public static readonly String SRC = "../../../resources/pdfs/form.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddExtraTable().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);
IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
fields["Name"].SetValue("Jeniffer");
fields["Company"].SetValue("iText's next customer");
fields["Country"].SetValue("No Man's Land");
form.FlattenFields();
Table table = new Table(UnitValue.CreatePercentArray(new float[] {1, 15}));
table.SetWidth(UnitValue.CreatePercentValue(80));
table.AddHeaderCell("#");
table.AddHeaderCell("description");
for (int i = 1; i <= 150; i++)
{
table.AddCell(i.ToString());
table.AddCell("test " + i);
}
// The custom renderer decreases the first page's area.
// As a result, there is not overlapping between the table from acroform and the new one.
doc.SetRenderer(new ExtraTableRenderer(doc));
doc.Add(table);
doc.Close();
}
protected class ExtraTableRenderer : DocumentRenderer
{
public ExtraTableRenderer(Document baseArg1)
: base(baseArg1)
{
}
// 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 ExtraTableRenderer(document);
}
protected override LayoutArea UpdateCurrentArea(LayoutResult overflowResult)
{
LayoutArea area = base.UpdateCurrentArea(overflowResult);
if (area.GetPageNumber() == 1)
{
area.GetBBox().DecreaseHeight(266);
}
return area;
}
}
}
}