Skip to main content
Skip table of contents

Filling out a form in multiple passes

This example was written in answer to the question Click How to tell iText which fields to flatten first?


fillformfieldorder

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.io.source.ByteArrayOutputStream;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.ReaderProperties;
import com.itextpdf.layout.Document;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class FillFormFieldOrder {
    public static final String DEST = "./target/sandbox/acroforms/fill_form_field_order.pdf";

    public static final String SRC = "./src/main/resources/pdfs/calendar_example.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new FillFormFieldOrder().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {

        // Partially flattened form's byte array with content, which should placed beneath the other content.
        byte[] tempForm = createPartiallyFlattenedForm();
        createResultantPdf(dest, tempForm);
    }

    public byte[] createPartiallyFlattenedForm() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos));
        Document doc = new Document(pdfDoc);
        PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, false);

        Map<String, PdfFormField> fields = form.getAllFormFields();
        fields.get("sunday_1").setValue("1");
        fields.get("sunday_2").setValue("2");
        fields.get("sunday_3").setValue("3");
        fields.get("sunday_4").setValue("4");
        fields.get("sunday_5").setValue("5");
        fields.get("sunday_6").setValue("6");

        // Add the field, identified by name, to the list of fields to be flattened
        form.partialFormFlattening("sunday_1");
        form.partialFormFlattening("sunday_2");
        form.partialFormFlattening("sunday_3");
        form.partialFormFlattening("sunday_4");
        form.partialFormFlattening("sunday_5");
        form.partialFormFlattening("sunday_6");

        // Only the included above fields are flattened.
        // If no fields have been explicitly included, then all fields are flattened.
        form.flattenFields();

        doc.close();

        return baos.toByteArray();
    }

    public void createResultantPdf(String dest, byte[] src) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(src),
                new ReaderProperties()), new PdfWriter(dest));
        PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);

        Map<String, PdfFormField> fields = form.getAllFormFields();
        fields.get("sunday_1_notes").setValue("It's Sunday today, let's go to the sea")
                .getFirstFormAnnotation().setBorderWidth(0);
        fields.get("sunday_2_notes").setValue("It's Sunday today, let's go to the park")
                .getFirstFormAnnotation().setBorderWidth(0);
        fields.get("sunday_3_notes").setValue("It's Sunday today, let's go to the beach")
                .getFirstFormAnnotation().setBorderWidth(0);
        fields.get("sunday_4_notes").setValue("It's Sunday today, let's go to the woods")
                .getFirstFormAnnotation().setBorderWidth(0);
        fields.get("sunday_5_notes").setValue("It's Sunday today, let's go to the lake")
                .getFirstFormAnnotation().setBorderWidth(0);
        fields.get("sunday_6_notes").setValue("It's Sunday today, let's go to the river")
                .getFirstFormAnnotation().setBorderWidth(0);

        // All fields will be flattened, because no fields have been explicitly included
        form.flattenFields();

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.IO.Source;
using iText.Kernel.Pdf;
using iText.Layout;

namespace iText.Samples.Sandbox.Acroforms
{
    public class FillFormFieldOrder
    {
        public static readonly String DEST = "results/sandbox/acroforms/fill_form_field_order.pdf";

        public static readonly String SRC = "../../../resources/pdfs/calendar_example.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new FillFormFieldOrder().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            
            // Get partially flattened form's byte array with content, which should be placed beneath the other content.
            byte[] tempForm = CreatePartiallyFlattenedForm();
            CreateResultantPdf(dest, tempForm);
        }

        public virtual byte[] CreatePartiallyFlattenedForm()
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos));
            Document doc = new Document(pdfDoc);
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, false);

            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            fields["sunday_1"].SetValue("1");
            fields["sunday_2"].SetValue("2");
            fields["sunday_3"].SetValue("3");
            fields["sunday_4"].SetValue("4");
            fields["sunday_5"].SetValue("5");
            fields["sunday_6"].SetValue("6");

            // Add the fields, identified by name, to the list of fields to be flattened
            form.PartialFormFlattening("sunday_1");
            form.PartialFormFlattening("sunday_2");
            form.PartialFormFlattening("sunday_3");
            form.PartialFormFlattening("sunday_4");
            form.PartialFormFlattening("sunday_5");
            form.PartialFormFlattening("sunday_6");

            // Only the included above fields are flattened.
            // If no fields have been explicitly included, then all fields are flattened.
            form.FlattenFields();

            doc.Close();

            return baos.ToArray();
        }

        public virtual void CreateResultantPdf(String dest, byte[] src)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().CreateSource(src),
                new ReaderProperties()), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);

            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            fields["sunday_1_notes"].SetValue("It's Sunday today, let's go to the sea").GetFirstFormAnnotation().SetBorderWidth(0);
            fields["sunday_2_notes"].SetValue("It's Sunday today, let's go to the park").GetFirstFormAnnotation().SetBorderWidth(0);
            fields["sunday_3_notes"].SetValue("It's Sunday today, let's go to the beach").GetFirstFormAnnotation().SetBorderWidth(0);
            fields["sunday_4_notes"].SetValue("It's Sunday today, let's go to the woods").GetFirstFormAnnotation().SetBorderWidth(0);
            fields["sunday_5_notes"].SetValue("It's Sunday today, let's go to the lake").GetFirstFormAnnotation().SetBorderWidth(0);
            fields["sunday_6_notes"].SetValue("It's Sunday today, let's go to the river").GetFirstFormAnnotation().SetBorderWidth(0);

            // All fields will be flattened, because no fields have been explicitly included
            form.FlattenFields();
            
            pdfDoc.Close();
        }
    }
}


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.