Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » VB.NET » Windows Forms » Downloads
 
Add your own
alternative version

Programmatically Complete PDF Form Fields using Visual Basic and the iTextSharp DLL

, 22 Jan 2008
Article describing how to programmatically complete PDF form fields with Visual Basic and the iTextSharp DLL
pdfgenerator_vb.zip
PdfGenerator_VB
fw4.pdf
PdfGenerator_VB
bin
Release
itextsharp.dll
PdfGenerator_VB.exe
PdfGenerator_VB.pdb
My Project
Application.myapp
Settings.settings
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.xml
Imports System.IO


Public Class Form1


    ''' <summary>
    ''' Application main form Load event handler
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Load all field names from template PDF
        ListFieldNames()

        ' Fill the target PDF form with canned values
        FillForm()

    End Sub



    ''' <summary>
    ''' List all of the form fields into a textbox.  The
    ''' form fields identified can be used to map each of the
    ''' fields in a PDF.
    ''' </summary>
    Private Sub ListFieldNames()

        Dim pdfTemplate As String = "c:\Temp\PDF\fw4.pdf"

        ' title the form
        Me.Text += " - " + PdfTemplate

        ' create a new PDF reader based on the PDF template document
        Dim pdfReader As PdfReader = New PdfReader(pdfTemplate)

        ' create and populate a string builder with each of the 
        ' field names available in the subject PDF
        Dim sb As New StringBuilder()

        Dim de As New DictionaryEntry
        For Each de In pdfReader.AcroFields.Fields
            sb.Append(de.Key.ToString() + Environment.NewLine)
        Next

        ' Write the string builder's content to the form's textbox
        textBox1.Text = sb.ToString()
        textBox1.SelectionStart = 0

    End Sub




    Private Sub FillForm()

        Dim pdfTemplate As String = "c:\Temp\PDF\fw4.pdf"
        Dim newFile As String = "c:\Temp\PDF\Final_fw4.pdf"

        Dim pdfReader As New PdfReader(pdfTemplate)
        Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
                    newFile, FileMode.Create))

        Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

        ' set form pdfFormFields

        ' The first worksheet and W-4 form
        pdfFormFields.SetField("f1_01(0)", "1")
        pdfFormFields.SetField("f1_02(0)", "1")
        pdfFormFields.SetField("f1_03(0)", "1")
        pdfFormFields.SetField("f1_04(0)", "8")
        pdfFormFields.SetField("f1_05(0)", "0")
        pdfFormFields.SetField("f1_06(0)", "1")
        pdfFormFields.SetField("f1_07(0)", "16")
        pdfFormFields.SetField("f1_08(0)", "28")
        pdfFormFields.SetField("f1_09(0)", "Franklin A.")
        pdfFormFields.SetField("f1_10(0)", "Benefield")
        pdfFormFields.SetField("f1_11(0)", "532")
        pdfFormFields.SetField("f1_12(0)", "12")
        pdfFormFields.SetField("f1_13(0)", "1234")

        ' The form's checkboxes
        pdfFormFields.SetField("c1_01(0)", "0")
        pdfFormFields.SetField("c1_02(0)", "Yes")
        pdfFormFields.SetField("c1_03(0)", "0")
        pdfFormFields.SetField("c1_04(0)", "Yes")

        ' The rest of the form pdfFormFields
        pdfFormFields.SetField("f1_14(0)", "100 North Cujo Street")
        pdfFormFields.SetField("f1_15(0)", "Nome, AK  67201")
        pdfFormFields.SetField("f1_16(0)", "9")
        pdfFormFields.SetField("f1_17(0)", "10")
        pdfFormFields.SetField("f1_18(0)", "11")
        pdfFormFields.SetField("f1_19(0)", "Walmart, Nome, AK")
        pdfFormFields.SetField("f1_20(0)", "WAL666")
        pdfFormFields.SetField("f1_21(0)", "AB")
        pdfFormFields.SetField("f1_22(0)", "4321")

        ' Second Worksheets pdfFormFields
        ' In order to map the fields, I just pass them a sequential
        ' number to mark them once I know which field is which, I 
        ' can pass the appropriate value
        pdfFormFields.SetField("f2_01(0)", "1")
        pdfFormFields.SetField("f2_02(0)", "2")
        pdfFormFields.SetField("f2_03(0)", "3")
        pdfFormFields.SetField("f2_04(0)", "4")
        pdfFormFields.SetField("f2_05(0)", "5")
        pdfFormFields.SetField("f2_06(0)", "6")
        pdfFormFields.SetField("f2_07(0)", "7")
        pdfFormFields.SetField("f2_08(0)", "8")
        pdfFormFields.SetField("f2_09(0)", "9")
        pdfFormFields.SetField("f2_10(0)", "10")
        pdfFormFields.SetField("f2_11(0)", "11")
        pdfFormFields.SetField("f2_12(0)", "12")
        pdfFormFields.SetField("f2_13(0)", "13")
        pdfFormFields.SetField("f2_14(0)", "14")
        pdfFormFields.SetField("f2_15(0)", "15")
        pdfFormFields.SetField("f2_16(0)", "16")
        pdfFormFields.SetField("f2_17(0)", "17")
        pdfFormFields.SetField("f2_18(0)", "18")
        pdfFormFields.SetField("f2_19(0)", "19")

        ' report by reading values from completed PDF
        Dim sTmp As String = "W-4 Completed for " + pdfFormFields.GetField("f1_09(0)") + " " + _
        pdfFormFields.GetField("f1_10(0)")
        MessageBox.Show(sTmp, "Finished")

        ' flatten the form to remove editting options, set it to false
        ' to leave the form open to subsequent manual edits
        pdfStamper.FormFlattening = True

        ' close the pdf
        pdfStamper.Close()

    End Sub


End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 22 Jan 2008
Article Copyright 2008 by salysle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid