Skip to main content
Skip table of contents

Forms in iText Core 8.0.0

Creating form fields using AcroForms is now much improved with iText Core 8.0.0. Rather than needing to specify the exact position of form elements, you can instead take advantage of iText's layout mechanism to add elements in a convenient and logical manner. The old method using the PdfAcroForm class is still supported, but this new approach makes the design and maintenance of forms much easier.

Text Form Fields can now be created through our Layout mechanism:

All you have to do is call the setInteractive(true) method on any form element and provide a 'true' argument to make the field interactive. Then, you can set layout properties to the form element. This change was made in order to optimize a developer's workflow when it comes to adding form fields to a PDF. 

Here is a quick example showing how to create a TextArea class and add layout properties:

Java
JAVA
import com.itextpdf.forms.form.FormProperty;
import com.itextpdf.forms.form.element.TextArea;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.properties.Property;
import com.itextpdf.layout.properties.UnitValue;

import java.io.FileNotFoundException;

public class Main {
    
	private static final String DEST = "LayoutFormFields.pdf";

	public static void main(String[] args) {
        createTextArea(DEST);
    }

    public static void createTextArea(String dest){
        try (Document document = new Document(new PdfDocument(new PdfWriter(dest)))){
            TextArea flattenTextArea = new TextArea("EXAMPLE TEXT AREA");
            flattenTextArea.setInteractive(true);
            flattenTextArea.setProperty(FormProperty.FORM_FIELD_VALUE, "Flatten\ntext area\nwith height");
            flattenTextArea.setProperty(Property.HEIGHT, new UnitValue(UnitValue.POINT,100));
            flattenTextArea.setProperty(Property.BORDER, new SolidBorder(2f));
            document.add(flattenTextArea);
            document.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
C#
C#
using iText.Forms.Form;
using iText.Forms.Form.Element;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Properties;

namespace iText8._0._0Examples
{
    internal class Program
    {
		
		private static String DEST = "LayoutFormFields.pdf";

		public static void Main(string[] args)
        {
            CreateInteractiveTextArea(DEST);
        }

        public static void CreateInteractiveTextArea(String dest)
        {
            using (Document document = new Document(new PdfDocument(new PdfWriter(dest))))
            {
                TextArea flattenTextArea = new TextArea("EXAMPLE TEXT AREA");
                flattenTextArea.SetInteractive(true);
                flattenTextArea.SetProperty(FormProperty.FORM_FIELD_VALUE, "Flatten\ntext area\nwith height");
                flattenTextArea.SetProperty(Property.HEIGHT, new UnitValue(UnitValue.POINT,100));
                flattenTextArea.SetProperty(Property.BORDER, new SolidBorder(2f));
                document.Add(flattenTextArea);
                document.Close();   
            }
        }
    }
}

For information on other improvements relating to the functionalities of AcroForms in iText 8.0.0, you can refer to our Forms Related Change Log. Happy Coding!

JavaScript errors detected

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

If this problem persists, please contact our support.