Fill Form

This example demonstrates how to fill data to fields of PDF document.

If you have a pdf form with textboxes like you can dymaically fill those forms using code .

We preset the path in code of the pdf with textboxes

Then you can acess each field in code and set any text you like .

Clicking generate button launches a pdf with those filled fields

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Forms;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class FillFormExample : ExampleBase
    {
        public FillFormExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {
        }

        public FillFormExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {
        }

        public override string Execute()
        {
            PdfDocument document = new PdfDocument();
            using (PdfImportedDocument doc = new PdfImportedDocument(CommonDataPath + "\\PDFForm.pdf"))
            {
                PdfImportedForm form = doc.Form;

                // fill the fields from the first page
                ((PdfImportedTextBoxField)form.Fields["f1-1"]).Text = "1";
                ((PdfImportedTextBoxField)form.Fields["f1-2"]).Text = "1";
                ((PdfImportedTextBoxField)form.Fields["f1-3"]).Text = "1";
                ((PdfImportedTextBoxField)form.Fields["f1-4"]).Text = "3";
                ((PdfImportedTextBoxField)form.Fields["f1-5"]).Text = "1";
                ((PdfImportedTextBoxField)form.Fields["f1-6"]).Text = "1";
                ((PdfImportedTextBoxField)form.Fields["f1-7"]).Text = "22";
                ((PdfImportedTextBoxField)form.Fields["f1-8"]).Text = "30";
                ((PdfImportedTextBoxField)form.Fields["f1-9"]).Text = "John";
                ((PdfImportedTextBoxField)form.Fields["f1-10"]).Text = "Doe";
                ((PdfImportedTextBoxField)form.Fields["f1-11"]).Text = "1 Microsoft Way";
                ((PdfImportedTextBoxField)form.Fields["f1-12"]).Text = "Redmond, WA";
                ((PdfImportedTextBoxField)form.Fields["f1-13"]).Text = "332";
                ((PdfImportedTextBoxField)form.Fields["f1-14"]).Text = "43";
                ((PdfImportedTextBoxField)form.Fields["f1-15"]).Text = "4556";
                ((PdfImportedTextBoxField)form.Fields["f1-16"]).Text = "3";
                ((PdfImportedTextBoxField)form.Fields["f1-17"]).Text = "2000";
                ((PdfImportedTextBoxField)form.Fields["f1-18"]).Text = "Exempt";
                ((PdfImportedTextBoxField)form.Fields["f1-19"]).Text = "Microsoft";
                ((PdfImportedTextBoxField)form.Fields["f1-20"]).Text = "200";
                ((PdfImportedTextBoxField)form.Fields["f1-21"]).Text = "22";
                ((PdfImportedTextBoxField)form.Fields["f1-22"]).Text = "56654";
                ((PdfImportedCheckBoxField)form.Fields["c1-1[0]"]).Items[0].Checked = true;
                ((PdfImportedCheckBoxField)form.Fields["c1-1[1]"]).Items[0].Checked = true;

                // fill the fields from the second page
                ((PdfImportedTextBoxField)form.Fields["f2-1"]).Text = "3200";
                ((PdfImportedTextBoxField)form.Fields["f2-2"]).Text = "4850";
                ((PdfImportedTextBoxField)form.Fields["f2-3"]).Text = "0";
                ((PdfImportedTextBoxField)form.Fields["f2-4"]).Text = "500";
                ((PdfImportedTextBoxField)form.Fields["f2-5"]).Text = "500";
                ((PdfImportedTextBoxField)form.Fields["f2-6"]).Text = "800";
                ((PdfImportedTextBoxField)form.Fields["f2-7"]).Text = "0";
                ((PdfImportedTextBoxField)form.Fields["f2-8"]).Text = "0";
                ((PdfImportedTextBoxField)form.Fields["f2-9"]).Text = "3";
                ((PdfImportedTextBoxField)form.Fields["f2-10"]).Text = "3";
                ((PdfImportedTextBoxField)form.Fields["f2-11"]).Text = "3";
                ((PdfImportedTextBoxField)form.Fields["f2-12"]).Text = "2";
                ((PdfImportedTextBoxField)form.Fields["f2-13"]).Text = "3";
                ((PdfImportedTextBoxField)form.Fields["f2-14"]).Text = "3";
                ((PdfImportedTextBoxField)form.Fields["f2-15"]).Text = "2";
                ((PdfImportedTextBoxField)form.Fields["f2-16"]).Text = "1";
                ((PdfImportedTextBoxField)form.Fields["f2-17"]).Text = "200";
                ((PdfImportedTextBoxField)form.Fields["f2-18"]).Text = "600";
                ((PdfImportedTextBoxField)form.Fields["f2-19"]).Text = "250";

                if (!Directory.Exists(OutputDir))
                    Directory.CreateDirectory(OutputDir);

                // Save the document to a file.
                //doc.Save(Outputfile);
                var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
                doc.Save(outputPath);
                doc.Close(true);
                return outputPath;
            }
        }
    }
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Forms
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
	Friend Class FillFormExample
		Inherits ExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)
		End Sub

		Public Overrides Function Execute() As String
			Dim document As New PdfDocument()
			Using doc As New PdfImportedDocument(CommonDataPath & "\PDFForm.pdf")
				Dim form As PdfImportedForm = doc.Form

				' fill the fields from the first page
				CType(form.Fields("f1-1"), PdfImportedTextBoxField).Text = "1"
				CType(form.Fields("f1-2"), PdfImportedTextBoxField).Text = "1"
				CType(form.Fields("f1-3"), PdfImportedTextBoxField).Text = "1"
				CType(form.Fields("f1-4"), PdfImportedTextBoxField).Text = "3"
				CType(form.Fields("f1-5"), PdfImportedTextBoxField).Text = "1"
				CType(form.Fields("f1-6"), PdfImportedTextBoxField).Text = "1"
				CType(form.Fields("f1-7"), PdfImportedTextBoxField).Text = "22"
				CType(form.Fields("f1-8"), PdfImportedTextBoxField).Text = "30"
				CType(form.Fields("f1-9"), PdfImportedTextBoxField).Text = "John"
				CType(form.Fields("f1-10"), PdfImportedTextBoxField).Text = "Doe"
				CType(form.Fields("f1-11"), PdfImportedTextBoxField).Text = "1 Microsoft Way"
				CType(form.Fields("f1-12"), PdfImportedTextBoxField).Text = "Redmond, WA"
				CType(form.Fields("f1-13"), PdfImportedTextBoxField).Text = "332"
				CType(form.Fields("f1-14"), PdfImportedTextBoxField).Text = "43"
				CType(form.Fields("f1-15"), PdfImportedTextBoxField).Text = "4556"
				CType(form.Fields("f1-16"), PdfImportedTextBoxField).Text = "3"
				CType(form.Fields("f1-17"), PdfImportedTextBoxField).Text = "2000"
				CType(form.Fields("f1-18"), PdfImportedTextBoxField).Text = "Exempt"
				CType(form.Fields("f1-19"), PdfImportedTextBoxField).Text = "Microsoft"
				CType(form.Fields("f1-20"), PdfImportedTextBoxField).Text = "200"
				CType(form.Fields("f1-21"), PdfImportedTextBoxField).Text = "22"
				CType(form.Fields("f1-22"), PdfImportedTextBoxField).Text = "56654"
				CType(form.Fields("c1-1[0]"), PdfImportedCheckBoxField).Items(0).Checked = True
				CType(form.Fields("c1-1[1]"), PdfImportedCheckBoxField).Items(0).Checked = True

				' fill the fields from the second page
				CType(form.Fields("f2-1"), PdfImportedTextBoxField).Text = "3200"
				CType(form.Fields("f2-2"), PdfImportedTextBoxField).Text = "4850"
				CType(form.Fields("f2-3"), PdfImportedTextBoxField).Text = "0"
				CType(form.Fields("f2-4"), PdfImportedTextBoxField).Text = "500"
				CType(form.Fields("f2-5"), PdfImportedTextBoxField).Text = "500"
				CType(form.Fields("f2-6"), PdfImportedTextBoxField).Text = "800"
				CType(form.Fields("f2-7"), PdfImportedTextBoxField).Text = "0"
				CType(form.Fields("f2-8"), PdfImportedTextBoxField).Text = "0"
				CType(form.Fields("f2-9"), PdfImportedTextBoxField).Text = "3"
				CType(form.Fields("f2-10"), PdfImportedTextBoxField).Text = "3"
				CType(form.Fields("f2-11"), PdfImportedTextBoxField).Text = "3"
				CType(form.Fields("f2-12"), PdfImportedTextBoxField).Text = "2"
				CType(form.Fields("f2-13"), PdfImportedTextBoxField).Text = "3"
				CType(form.Fields("f2-14"), PdfImportedTextBoxField).Text = "3"
				CType(form.Fields("f2-15"), PdfImportedTextBoxField).Text = "2"
				CType(form.Fields("f2-16"), PdfImportedTextBoxField).Text = "1"
				CType(form.Fields("f2-17"), PdfImportedTextBoxField).Text = "200"
				CType(form.Fields("f2-18"), PdfImportedTextBoxField).Text = "600"
				CType(form.Fields("f2-19"), PdfImportedTextBoxField).Text = "250"

				If Not Directory.Exists(OutputDir) Then
					Directory.CreateDirectory(OutputDir)
				End If

				' Save the document to a file.
				'doc.Save(Outputfile);
				Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
				doc.Save(outputPath)
				doc.Close(True)
				Return outputPath
			End Using
		End Function
	End Class
End Namespace