Fill PDF form

Manipulate PDF, PDF forms
2/20/2014

Downloads

This code sample shows how to assign values to various types of PDF form fields.

C#

1 using (var fileIn = new FileStream("form.pdf", FileMode.Open, FileAccess.Read)) 2 { 3 // open PDF 4 Document document = new Document(fileIn); 5 6 // set the value of text field 7 TextField textField = document.Fields["Text1"] as TextField; 8 textField.Value = "Hello"; 9 10 // check check box 11 CheckBoxField checkBox = document.Fields["CheckBox2"] as CheckBoxField; 12 checkBox.CheckBoxValue = CheckState.On; 13 14 // select second radio button option 15 // (range checking omitted for the purpose of this code sample) 16 RadioButtonField radioButton = document.Fields["Radio Button4"] as RadioButtonField; 17 radioButton.RadioButtonValue = radioButton.Options[0]; //select first option 18 19 // select second item in list box 20 ListBoxField listBox = document.Fields["ListBox7"] as ListBoxField; 21 listBox.ListBoxValue = new ListOption[] { listBox.Options[1] }; // select second option 22 23 // set the value of a drop down 24 DropDownListField dropDown = document.Fields["ComboBox8"] as DropDownListField; 25 dropDown.DropDownListValue = dropDown.Options[1]; // select second option 26 27 // save PDF 28 using (var fileOut = new FileStream("filled.pdf", FileMode.Create, FileAccess.Write)) 29 { 30 document.Write(fileOut); 31 } 32 }

VB.NET code sample

1 Using fileIn As New FileStream("..\..\..\inputDocuments\fields.pdf", FileMode.Open, FileAccess.Read) 2 Dim document As New Document(fileIn) 3 4 Dim textField As TextField = TryCast(document.Fields("Text1"), TextField) 5 textField.Value = "Hello" 6 7 Dim checkBox As CheckBoxField = TryCast(document.Fields("Check Box2"), CheckBoxField) 8 checkBox.CheckBoxValue = CheckState.[On] 9 10 Dim radioButton As RadioButtonField = TryCast(document.Fields("Radio Button4"), RadioButtonField) 11 radioButton.RadioButtonValue = radioButton.Options(1) 12 ' second option 13 Dim listBox As ListBoxField = TryCast(document.Fields("List Box7"), ListBoxField) 14 listBox.ListBoxValue = New ListOption() {listBox.Options(1)} 15 ' second option 16 Dim dropDown As DropDownListField = TryCast(document.Fields("Combo Box8"), DropDownListField) 17 dropDown.DropDownListValue = dropDown.Options(1) 18 ' second option 19 Using fileOut As New FileStream("..\..\fillfields.pdf", FileMode.Create, FileAccess.Write) 20 document.Write(fileOut) 21 End Using 22 End Using