Skip to main content
Skip table of contents

iText 7 jump-start tutorial : Chapter 4


c04e01_01_textannotation

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 tutorial.chapter04;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;

/**
 * Simple text annotation example.
 */
public class C04E01_01_TextAnnotation {

    public static final String DEST = "results/chapter04/text_annotation.pdf";

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

    public void createPdf(String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        //Initialize document
        Document document = new Document(pdf);
        document.add(new Paragraph("The example of text annotation."));

        //Create text annotation
        PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(20, 800, 0, 0))
                .setOpen(true)
                .setColor(ColorConstants.GREEN)
                .setTitle(new PdfString("iText"))
                .setContents("With iText, you can truly take your documentation needs to the next level.");
        pdf.getFirstPage().addAnnotation(ann);

        //Close document
        document.close();

    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Layout;
using iText.Layout.Element;

namespace Tutorial.Chapter04 {
    /// <summary>Simple text annotation example.</summary>
    public class C04E01_01_TextAnnotation {
        public const String DEST = "../../../results/chapter04/text_annotation.pdf";

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

        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            //Initialize document
            Document document = new Document(pdf);
            document.Add(new Paragraph("The example of text annotation."));
            //Create text annotation
            PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(20, 800, 0, 0))
                .SetOpen(true)
                .SetColor(ColorConstants.GREEN)
                .SetTitle(new PdfString("iText"))
                .SetContents("With iText, you can truly take your documentation needs to the next level.");
            pdf.GetFirstPage().AddAnnotation(ann);
            //Close document
            document.Close();
        }
    }
}


c04e01_02_linkannotation

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 tutorial.chapter04;

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;

/**
 * Simple link annotation example.
 */
public class C04E01_02_LinkAnnotation {

    public static final String DEST = "results/chapter04/link_annotation.pdf";

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

    public void createPdf(String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        //Initialize document
        Document document = new Document(pdf);

        //Create link annotation
        PdfLinkAnnotation annotation = new PdfLinkAnnotation(new Rectangle(0, 0))
                .setAction(PdfAction.createURI("http://itextpdf.com/"));
        Link link = new Link("here", annotation);
        Paragraph p = new Paragraph("The example of link annotation. Click ")
                .add(link.setUnderline())
                .add(" to learn more...");
        document.add(p);

        //Close document
        document.close();

    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Annot;
using iText.Layout;
using iText.Layout.Element;

namespace Tutorial.Chapter04 {
    /// <summary>Simple link annotation example.</summary>
    public class C04E01_02_LinkAnnotation {
        public const String DEST = "../../../results/chapter04/link_annotation.pdf";

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

        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            //Initialize document
            Document document = new Document(pdf);
            //Create link annotation
            PdfLinkAnnotation annotation = ((PdfLinkAnnotation)new PdfLinkAnnotation(new Rectangle(0, 0)).SetAction(PdfAction
                .CreateURI("http://itextpdf.com/")));
            Link link = new Link("here", annotation);
            Paragraph p = new Paragraph("The example of link annotation. Click ").Add(link.SetUnderline()).Add(" to learn more..."
                );
            document.Add(p);
            //Close document
            document.Close();
        }
    }
}


c04e01_03_lineannotation

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 tutorial.chapter04;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfLineAnnotation;
import java.io.File;
import java.io.IOException;

/**
 * Simple line annotation example.
 */
public class C04E01_03_LineAnnotation {

    public static final String DEST = "results/chapter04/line_annotation.pdf";

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

    public void createPdf(String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        PdfPage page = pdf.addNewPage();

        PdfArray lineEndings = new PdfArray();
        lineEndings.add(new PdfName("Diamond"));
        lineEndings.add(new PdfName("Diamond"));

        //Create line annotation with inside caption
        PdfAnnotation annotation = new PdfLineAnnotation(
            new Rectangle(0, 0),
            new float[]{20, 790, page.getPageSize().getWidth() - 20, 790})
                .setLineEndingStyles((lineEndings))
                .setContentsAsCaption(true)
                .setTitle(new PdfString("iText"))
                .setContents("The example of line annotation")
                .setColor(ColorConstants.BLUE);
        page.addAnnotation(annotation);

        //Close document
        pdf.close();

    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;

namespace Tutorial.Chapter04 {
    /// <summary>Simple line annotation example.</summary>
    public class C04E01_03_LineAnnotation {
        public const String DEST = "../../../results/chapter04/line_annotation.pdf";

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

        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            PdfPage page = pdf.AddNewPage();
            PdfArray lineEndings = new PdfArray();
            lineEndings.Add(new PdfName("Diamond"));
            lineEndings.Add(new PdfName("Diamond"));
            //Create line annotation with inside caption
            PdfAnnotation annotation = new PdfLineAnnotation(new Rectangle(0, 0), new float[] { 20, 790, page.GetPageSize
                ().GetWidth() - 20, 790 }).SetLineEndingStyles((lineEndings)).SetContentsAsCaption(true).SetTitle(new 
                PdfString("iText")).SetContents("The example of line annotation").SetColor(ColorConstants.BLUE);
            page.AddAnnotation(annotation);
            //Close document
            pdf.Close();
        }
    }
}


c04e01_04_textmarkupannotation

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 tutorial.chapter04;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextMarkupAnnotation;
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;
import java.io.IOException;

/**
 * Simple text markup annotation example.
 */
public class C04E01_04_TextMarkupAnnotation {

    public static final String DEST = "results/chapter04/textmarkup_annotation.pdf";

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

    public void createPdf(String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        //Initialize document
        Document document = new Document(pdf);

        Paragraph p = new Paragraph("The example of text markup annotation.");
        document.showTextAligned(p, 20, 795, 1, TextAlignment.LEFT,
                VerticalAlignment.MIDDLE, 0);

        //Create text markup annotation
        PdfAnnotation ann = PdfTextMarkupAnnotation.createHighLight(new Rectangle(105, 790, 64, 10),
                new float[]{169, 790, 105, 790, 169, 800, 105, 800})
                .setColor(ColorConstants.YELLOW)
                .setTitle(new PdfString("Hello!"))
                .setContents(new PdfString("I'm a popup."))
                .setTitle(new PdfString("iText"))
                .setRectangle(new PdfArray(new float[]{100, 600, 200, 100}));
        pdf.getFirstPage().addAnnotation(ann);

        //Close document
        document.close();

    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace Tutorial.Chapter04 {
    /// <summary>Simple text markup annotation example.</summary>
    public class C04E01_04_TextMarkupAnnotation {
        public const String DEST = "../../../results/chapter04/textmarkup_annotation.pdf";

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

        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            //Initialize document
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("The example of text markup annotation.");
            document.ShowTextAligned(p, 20, 795, 1, TextAlignment.LEFT, VerticalAlignment.MIDDLE, 0);
            //Create text markup annotation
            PdfAnnotation ann = PdfTextMarkupAnnotation.CreateHighLight(new Rectangle(105, 790, 64, 10), new float[] { 
                169, 790, 105, 790, 169, 800, 105, 800 }).SetColor(ColorConstants.YELLOW).SetTitle(new PdfString("Hello!")).SetContents
                (new PdfString("I'm a popup.")).SetTitle(new PdfString("iText")).SetRectangle(new PdfArray
                (new float[] { 100, 600, 200, 100 }));
            pdf.GetFirstPage().AddAnnotation(ann);
            //Close document
            document.Close();
        }
    }
}


c04e02_jobapplication

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 tutorial.chapter04;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfChoiceFormField;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.forms.fields.TextFormFieldBuilder;
import com.itextpdf.forms.fields.CheckBoxFormFieldBuilder;
import com.itextpdf.forms.fields.ChoiceFormFieldBuilder;
import com.itextpdf.forms.fields.PushButtonFormFieldBuilder;
import com.itextpdf.forms.fields.RadioFormFieldBuilder;
import com.itextpdf.forms.fields.properties.CheckBoxType;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import java.io.File;
import java.io.IOException;

/**
 * Simple widget annotation example.
 */
public class C04E02_JobApplication {

    public static final String DEST = "results/chapter04/job_application.pdf";

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

    public void createPdf(String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        PageSize ps = PageSize.A4;
        pdf.setDefaultPageSize(ps);

        // Initialize document
        Document document = new Document(pdf);

        C04E02_JobApplication.addAcroForm(document);

        //Close document
        document.close();

    }

    public static PdfAcroForm addAcroForm(Document doc) {

        Paragraph title = new Paragraph("Application for employment")
                .setTextAlignment(TextAlignment.CENTER)
                .setFontSize(16);
        doc.add(title);
        doc.add(new Paragraph("Full name:").setFontSize(12));
        doc.add(new Paragraph("Native language:      English         French       German        Russian        Spanish").setFontSize(12));
        doc.add(new Paragraph("Experience in:       cooking        driving           software development").setFontSize(12));
        doc.add(new Paragraph("Preferred working shift:").setFontSize(12));
        doc.add(new Paragraph("Additional information:").setFontSize(12));

        //Add acroform
        PdfAcroForm form = PdfFormCreator.getAcroForm(doc.getPdfDocument(), true);

        //Create text field
        PdfTextFormField nameField = new TextFormFieldBuilder(doc.getPdfDocument(), "name")
                .setWidgetRectangle(new Rectangle(99, 753, 425, 15)).createText();
        nameField.setValue("");
        form.addField(nameField);

        //Create radio buttons
        RadioFormFieldBuilder builder = new RadioFormFieldBuilder(doc.getPdfDocument(), "language");
        PdfButtonFormField group = builder.createRadioGroup();
        group.setValue("", true);
        group.addKid(builder.createRadioButton("English", new Rectangle(130, 728, 15, 15)));
        group.addKid(builder.createRadioButton("French", new Rectangle(200, 728, 15, 15)));
        group.addKid(builder.createRadioButton("German", new Rectangle(260, 728, 15, 15)));
        group.addKid(builder.createRadioButton("Russian", new Rectangle(330, 728, 15, 15)));
        group.addKid(builder.createRadioButton("Spanish", new Rectangle(400, 728, 15, 15)));
        form.addField(group);

        //Create checkboxes
        for (int i = 0; i < 3; i++) {
            PdfButtonFormField checkField = new CheckBoxFormFieldBuilder(doc.getPdfDocument(), "experience".concat(String.valueOf(i + 1)))
                    .setWidgetRectangle(new Rectangle(119 + i * 69, 701, 15, 15))
                    .setCheckType(CheckBoxType.CHECK).createCheckBox();
            checkField.setValue("Off", true);
            form.addField(checkField);
        }

        //Create combobox
        String[] options = {"Any", "6.30 am - 2.30 pm", "1.30 pm - 9.30 pm"};
        PdfChoiceFormField choiceField = new ChoiceFormFieldBuilder(doc.getPdfDocument(), "shift")
                .setWidgetRectangle(new Rectangle(163, 676, 115, 15))
                .setOptions(options).createComboBox();
        choiceField.setValue("Any", true);
        form.addField(choiceField);

        //Create multiline text field
        PdfTextFormField infoField = new TextFormFieldBuilder(doc.getPdfDocument(), "info")
                .setWidgetRectangle(new Rectangle(158, 625, 366, 40)).createMultilineText();
        infoField.setValue("");
        form.addField(infoField);

        //Create push button field
        PdfButtonFormField button = new PushButtonFormFieldBuilder(doc.getPdfDocument(), "reset").setCaption("RESET")
                .setWidgetRectangle(new Rectangle(479, 594, 45, 15)).createPushButton();
        button.getFirstFormAnnotation().setAction(PdfAction.createResetForm(new String[] {"name", "language", "experience1", "experience2", "experience3", "shift", "info"}, 0));
        form.addField(button);

        return form;

    }
}

C#

C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Forms.Fields.Properties;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace Tutorial.Chapter04 {
    /// <summary>Simple widget annotation example.</summary>
    public class C04E02_JobApplication {
        public const String DEST = "../../../results/chapter04/job_application.pdf";

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

        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            PageSize ps = PageSize.A4;
            pdf.SetDefaultPageSize(ps);
            // Initialize document
            Document document = new Document(pdf);
            C04E02_JobApplication.AddAcroForm(document);
            //Close document
            document.Close();
        }

        public static PdfAcroForm AddAcroForm(Document doc) {
            Paragraph title = new Paragraph("Application for employment").SetTextAlignment(TextAlignment.CENTER).SetFontSize
                (16);
            doc.Add(title);
            doc.Add(new Paragraph("Full name:").SetFontSize(12));
            doc.Add(new Paragraph("Native language:      English         French       German        Russian        Spanish"
                ).SetFontSize(12));
            doc.Add(new Paragraph("Experience in:       cooking        driving           software development").SetFontSize
                (12));
            doc.Add(new Paragraph("Preferred working shift:").SetFontSize(12));
            doc.Add(new Paragraph("Additional information:").SetFontSize(12));
            //Add acroform
            PdfAcroForm form = PdfFormCreator.GetAcroForm(doc.GetPdfDocument(), true);
            //Create text field
            PdfTextFormField nameField = new TextFormFieldBuilder(doc.GetPdfDocument(), "name")
                .SetWidgetRectangle(new Rectangle(99, 753, 425, 15)).CreateText();
            nameField.SetValue("");
            form.AddField(nameField);
            //Create radio buttons
            RadioFormFieldBuilder builder = new RadioFormFieldBuilder(doc.GetPdfDocument(), "language");
            PdfButtonFormField group = builder.CreateRadioGroup();
            group.SetValue("");
            group.AddKid(builder.CreateRadioButton("English", new Rectangle(130, 728, 15, 15)));
            group.AddKid(builder.CreateRadioButton("French", new Rectangle(200, 728, 15, 15)));
            group.AddKid(builder.CreateRadioButton("German", new Rectangle(260, 728, 15, 15)));
            group.AddKid(builder.CreateRadioButton("Russian", new Rectangle(330, 728, 15, 15)));
            group.AddKid(builder.CreateRadioButton("Spanish", new Rectangle(400, 728, 15, 15)));
            form.AddField(group);                                                
            //Create checkboxes
            for (int i = 0; i < 3; i++) {
                PdfButtonFormField checkField = new CheckBoxFormFieldBuilder(doc.GetPdfDocument(),
                    String.Concat("experience", (i + 1).ToString()))
                    .SetWidgetRectangle(new Rectangle(119 + i * 69, 701, 15, 15))
                    .SetCheckType(CheckBoxType.CHECK).CreateCheckBox();
                checkField.SetValue("Off");
                form.AddField(checkField);
            }
            //Create combobox
            String[] options = new String[] { "Any", "6.30 am - 2.30 pm", "1.30 pm - 9.30 pm" };
            PdfChoiceFormField choiceField = new ChoiceFormFieldBuilder(doc.GetPdfDocument(), "shift")
                .SetWidgetRectangle(new Rectangle(163, 676, 115, 15)).SetOptions(options).CreateComboBox();
            choiceField.SetValue("Any");
            form.AddField(choiceField);
            //Create multiline text field
            PdfTextFormField infoField = new TextFormFieldBuilder(doc.GetPdfDocument(), "info")
                .SetWidgetRectangle(new Rectangle(158, 625, 366, 40)).CreateMultilineText();
            infoField.SetValue("");
            form.AddField(infoField);
            //Create push button field
            PdfButtonFormField button = new PushButtonFormFieldBuilder(doc.GetPdfDocument(), "reset")
                .SetWidgetRectangle(new Rectangle(479, 594, 45, 15)).SetCaption("RESET").CreatePushButton();
            button.GetFirstFormAnnotation().SetAction(PdfAction.CreateResetForm(new String[] { "name", "language", "experience1", "experience2"
                , "experience3", "shift", "info" }, 0));
            form.AddField(button);
            return form;
        }
    }
}


c04e03_createandfill

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 tutorial.chapter04;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Simple filling out form example.
 */
public class C04E03_CreateAndFill {

    public static final String DEST = "results/chapter04/create_and_fill.pdf";

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

    public void createPdf(String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document doc = new Document(pdf);

        PdfAcroForm form = C04E02_JobApplication.addAcroForm(doc);
        Map<String, PdfFormField> fields = form.getAllFormFields();
        fields.get("name").setValue("James Bond");
        fields.get("language").setValue("English");
        fields.get("experience1").setValue("Off");
        fields.get("experience2").setValue("Yes");
        fields.get("experience3").setValue("Yes");
        fields.get("shift").setValue("Any");
        fields.get("info").setValue("I was 38 years old when I became an MI6 agent.");

        doc.close();

    }
}

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;

namespace Tutorial.Chapter04 {
    /// <summary>Simple filling out form example.</summary>
    public class C04E03_CreateAndFill {
        public const String DEST = "../../../results/chapter04/create_and_fill.pdf";

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

        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document doc = new Document(pdf);
            PdfAcroForm form = C04E02_JobApplication.AddAcroForm(doc);
            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            PdfFormField toSet;
            fields.TryGetValue("name", out toSet);
            toSet.SetValue("James Bond");
            fields.TryGetValue("language", out toSet);
            toSet.SetValue("English");
            fields.TryGetValue("experience1", out toSet);
            toSet.SetValue("Off");
            fields.TryGetValue("experience2", out toSet);
            toSet.SetValue("Yes");
            fields.TryGetValue("experience3", out toSet);
            toSet.SetValue("Yes");
            fields.TryGetValue("shift", out toSet);
            toSet.SetValue("Any");
            fields.TryGetValue("info", out toSet);
            toSet.SetValue("I was 38 years old when I became an MI6 agent.");
            doc.Close();
        }
    }
}


c04e04_fillform

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 tutorial.chapter04;

import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import java.io.*;
import java.util.Map;

/**
 * Simple filling out form example.
 */
public class C04E04_FillForm {

    public static final String SRC = "src/main/resources/pdf/job_application.pdf";
    public static final String DEST = "results/chapter04/fill_form.pdf";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E04_FillForm().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));


        PdfAcroForm form = PdfFormCreator.getAcroForm(pdf, true);
        Map<String, PdfFormField> fields = form.getAllFormFields();
        fields.get("name").setValue("James Bond");
        fields.get("language").setValue("English");
        fields.get("experience1").setValue("Off");
        fields.get("experience2").setValue("Yes");
        fields.get("experience3").setValue("Yes");
        fields.get("shift").setValue("Any");
        fields.get("info").setValue("I was 38 years old when I became an MI6 agent.");

        pdf.close();

    }
}

C#

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

namespace Tutorial.Chapter04 {
    /// <summary>Simple filling out form example.</summary>
    public class C04E04_FillForm {
        public const String SRC = "../../../resources/pdf/job_application.pdf";

        public const String DEST = "../../../results/chapter04/fill_form.pdf";

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

        public virtual void ManipulatePdf(String src, String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdf, true);
            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            PdfFormField toSet;
            fields.TryGetValue("name", out toSet);
            toSet.SetValue("James Bond");
            fields.TryGetValue("language", out toSet);
            toSet.SetValue("English");
            fields.TryGetValue("experience1", out toSet);
            toSet.SetValue("Off");
            fields.TryGetValue("experience2", out toSet);
            toSet.SetValue("Yes");
            fields.TryGetValue("experience3", out toSet);
            toSet.SetValue("Yes");
            fields.TryGetValue("shift", out toSet);
            toSet.SetValue("Any");
            fields.TryGetValue("info", out toSet);
            toSet.SetValue("I was 38 years old when I became an MI6 agent.");
            pdf.Close();
        }
    }
}


c04e05_flattenform

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 tutorial.chapter04;

import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import java.io.*;
import java.util.Map;

/**
 * Simple filling out form example.
 */
public class C04E05_FlattenForm {

    public static final String SRC = "src/main/resources/pdf/job_application.pdf";
    public static final String DEST = "results/chapter04/flatten_form.pdf";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E05_FlattenForm().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws IOException {

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));


        PdfAcroForm form = PdfFormCreator.getAcroForm(pdf, true);
        Map<String, PdfFormField> fields = form.getAllFormFields();
        fields.get("name").setValue("James Bond");
        fields.get("language").setValue("English");
        fields.get("experience1").setValue("Off");
        fields.get("experience2").setValue("Yes");
        fields.get("experience3").setValue("Yes");
        fields.get("shift").setValue("Any");
        fields.get("info").setValue("I was 38 years old when I became an MI6 agent.");
        form.flattenFields();

        pdf.close();

    }
}

C#

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

namespace Tutorial.Chapter04 {
    /// <summary>Simple filling out form example.</summary>
    public class C04E05_FlattenForm {
        public const String SRC = "../../../resources/pdf/job_application.pdf";

        public const String DEST = "../../../results/chapter04/flatten_form.pdf";

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

        public virtual void ManipulatePdf(String src, String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdf, true);
            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            PdfFormField toSet;
            fields.TryGetValue("name", out toSet);
            toSet.SetValue("James Bond");
            fields.TryGetValue("language", out toSet);
            toSet.SetValue("English");
            fields.TryGetValue("experience1", out toSet);
            toSet.SetValue("Off");
            fields.TryGetValue("experience2", out toSet);
            toSet.SetValue("Yes");
            fields.TryGetValue("experience3", out toSet);
            toSet.SetValue("Yes");
            fields.TryGetValue("shift", out toSet);
            toSet.SetValue("Any");
            fields.TryGetValue("info", out toSet);
            toSet.SetValue("I was 38 years old when I became an MI6 agent.");
            form.FlattenFields();
            pdf.Close();
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.