SelectPdf for .NET - Pdf Form Fields Filling - VB.NET / ASP.NET MVC Sample

This sample shows how to use SelectPdf Library for .NET to load an existing document that contains an AcroForm PDF form, fill some of the fields and save the document.

Here is our test pdf document that contains a PDF form:
Test form

Click on the "Create PDF" button below to see the result.

Sample Code VB.NET

Imports SelectPdf

Namespace Controllers
    Public Class PdfFormFillingController
        Inherits Controller

        ' GET: PdfFormFilling
        Public Function Index() As ActionResult
            Return View()
        End Function

        <HttpPost> _
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' the initial file
            Dim file As String = Server.MapPath("~/files/w-9.pdf")

            ' load the pdf form manager
            Dim form As New PdfFormManager()

            ' fill some fields
            Dim name As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_1"), PdfFormFieldTextBox)
            name.Text = "This is my name"

            Dim businessName As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_2"), PdfFormFieldTextBox)
            businessName.Text = "This is my business name"

            Dim clasifIndividual As PdfFormFieldCheckBox =
                TryCast(form.Fields("c1_1"), PdfFormFieldCheckBox)
            clasifIndividual.Checked = True

            Dim address As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_7"), PdfFormFieldTextBox)
            address.Text = "This is my address"

            Dim city As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_8"), PdfFormFieldTextBox)
            city.Text = "This is my city"

            Dim employer1 As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_14"), PdfFormFieldTextBox)
            employer1.Text = "XX"

            Dim employer2 As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_15"), PdfFormFieldTextBox)
            employer2.Text = "1234567"

            Dim ssn1 As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_11"), PdfFormFieldTextBox)
            ssn1.Text = "123"

            Dim ssn2 As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_12"), PdfFormFieldTextBox)
            ssn2.Text = "45"

            Dim ssn3 As PdfFormFieldTextBox =
                TryCast(form.Fields("f1_13"), PdfFormFieldTextBox)
            ssn3.Text = "6789"

            ' save pdf document
            Dim pdf As Byte() = form.Save()

            ' close pdf document

            ' return resulted pdf document
            Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
            fileResult.FileDownloadName = "Document.pdf"
            Return fileResult
        End Function
    End Class
End Namespace