Filling in a PDF Form using PDFKit.Net from Tall Components
Introduction
PDFKit.Net allows you to access and edit all aspects of a PDF document. Here we demonstrate how to access the fields that are contained in the PDF form and add values to those fields before saving the file as a new document.
Detail
The project uses a simple predefined PDF form which contains several fields of different types - textboxes, multiline textboxes, radio buttons and a check box. A windows form is displayed which collects some data from the user and then opens the PDF form template, creates references to the fields in the form and populates their values using the user data.
Set up
Download the Zip file using the above link and unpack it onto your computer. The archive contains directories for VB + C#.Net projects. The PDFKit.Net class library is contained within both projects and will run in evaluation mode by displaying a notice in red at the top of all documents produced.
The PDF Form CustomerQuotation.pdf is contained in both projects. This form was created using Adobe Acrobat and has the following fields defined:-
- name - text box
- address - multi-line text box
- data - text box
- product - text box
- color - radio button group
- delivery - Checkbox
The complete form looks like this:
The application to capture the information looks like this:
The Code
The project contains a reference to TallComponents.PDF.Kit which his in the file TallComponents.PDF.Kit.Dll. This provides some namespaces which we can reference:
C#
using TallComponents.PDF;
using TallComponents.PDF.Forms.Fields;
VB
Imports TallComponents.PDF
Imports TallComponents.PDF.Forms.Fields
Most of the work is done in the btnCreate_Click event handler. First we create a Stream object called fileIn and then pass this to the constructor of the Document object called form. Document is the PDFKit object which provides access to all of the elements within the PDF document.
C#
private void btnCreate_Click(object sender, EventArgs e)
{
using (FileStream fileIn = new FileStream(@"..\..\CustomerQuotation.pdf", FileMode.Open, FileAccess.Read))
{
Document form = new Document(fileIn);
VB
Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreate.Click
Next we set up references to the various fields in the document wich are accessed via the Fields collection. The collection contains generic Field objects and so we need to assign them to a more specific type in order to access properties such as CheckBoxValue on check box type field.
C#
TextField nameBox = form.Fields["name"] as TextField;
TextField addressBox = form.Fields["address"] as TextField;
TextField dateBox = form.Fields["data"] as TextField;
TextField productBox = form.Fields["product"] as TextField;
RadioButtonField colorBox = form.Fields["color"] as RadioButtonField;
CheckBoxField deliveryBox = form.Fields["delivery"] as CheckBoxField;
VB
Dim nameBox As TextField = TryCast(form.Fields("name"), TextField)
Dim addressBox As TextField = TryCast(form.Fields("address"), TextField)
Dim dateBox As TextField = TryCast(form.Fields("data"), TextField)
Dim productBox As TextField = TryCast(form.Fields("product"), TextField)
Dim colorBox As RadioButtonField = TryCast(form.Fields("color"), RadioButtonField)
Dim deliveryBox As CheckBoxField = TryCast(form.Fields("delivery"), CheckBoxField)
Now we can populate the individual form fields with the values that the user entered on screen.
C#
nameBox.Value = txtName.Text;
addressBox.Value = txtAddress.Text;
dateBox.Value = DateTime.Now.ToShortDateString();
productBox.Value = cmbProduct.SelectedItem.ToString();
if (radRed.Checked)
colorBox.RadioButtonValue = colorBox.Options[0];
else if (radBlue.Checked)
colorBox.RadioButtonValue = colorBox.Options[1];
else if (radGreen.Checked)
colorBox.RadioButtonValue = colorBox.Options[2];
else if (radYellow.Checked)
colorBox.RadioButtonValue = colorBox.Options[3];
if (chkDeliver.Checked)
deliveryBox.CheckBoxValue = TallComponents.PDF.Forms.Fields.CheckState.On;
else
deliveryBox.CheckBoxValue = TallComponents.PDF.Forms.Fields.CheckState.Off;
VB
nameBox.Value = txtName.Text
addressBox.Value = txtAddress.Text
dateBox.Value = DateTime.Now.ToShortDateString()
productBox.Value = cmbProduct.SelectedItem.ToString()
If radRed.Checked Then
colorBox.RadioButtonValue = colorBox.Options(0)
ElseIf radBlue.Checked Then
colorBox.RadioButtonValue = colorBox.Options(1)
ElseIf radGreen.Checked Then
colorBox.RadioButtonValue = colorBox.Options(2)
ElseIf radYellow.Checked Then
colorBox.RadioButtonValue = colorBox.Options(3)
End If
If chkDeliver.Checked Then
deliveryBox.CheckBoxValue = TallComponents.PDF.Forms.Fields.CheckState.[On]
Else
deliveryBox.CheckBoxValue = TallComponents.PDF.Forms.Fields.CheckState.Off
End If
Finally we set up a new Stream to write the file out to disk and pass this to the Write method of the form object. This will create our new PDF document with the form fields completed.
C#
using (FileStream fileOut = new FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write))
{
form.Write(fileOut);
}
VB
Using fileOut As New FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write)
form.Write(fileOut)
End Using
Conclusion
This simple examples has shown how we can build an application which captures data and uses PDFKit.Net to populate a predefined PDF form, saving the output to a new file, ready for transmission or output.
PDFKit.Net has many other capabilities for manipulating PDF documents. For more samples please use the link below.
Visit
TallComponents BV
for more information and more samples.