Fill PDF Form

PDF forms are frequently used to collect data and information. For example, Questionnaires are usually designed to collect responses for survey purposes. You can fill, edit, modify PDF forms in C# programmatically as we interact with different fillable PDF forms in today’s digital world. Considering the huge scope and importance of PDF forms, Aspose.PDF for .NET API supports many features to work with PDF forms. Let us explore the following use cases using C# language in this article:

Create Fillable PDF Forms using C# Programmatically

You can create a fillable PDF form from scratch using Aspose.PDF for .NET API. Here we will consider the basic example of adding two TextBoxField instances and RadioButton. However, one of the TextBoxField is single line while the other one is multi-line. Below are the steps to create a form in PDF document:

  1. Create an instance of Document class
  2. Add a blank page in PDF document
  3. Add TextBox Field in the form
  4. Set different properties of the fields including Font, Border etc.
  5. Add Radio Button in the form
  6. Save PDF document

Following code snippet shows how to create form in PDF using C#:

Document pdfdoc = new Document();
Page page = pdfdoc.Pages.Add();
TextBoxField nameBox = new TextBoxField(pdfdoc, new Aspose.Pdf.Rectangle(275, 740, 440, 770));
nameBox.PartialName = "nameBox1";
nameBox.DefaultAppearance.FontSize = 10;
nameBox.Multiline = true;
Border nameBorder = new Border(nameBox);
nameBorder.Width = 1;
nameBox.Border = nameBorder;
nameBox.Characteristics.Border = System.Drawing.Color.Black;
nameBox.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
TextBoxField mrnBox = new TextBoxField(pdfdoc, new Aspose.Pdf.Rectangle(275, 718, 440, 738));
mrnBox.PartialName = "Box1";
mrnBox.DefaultAppearance.FontSize = 10;
Border mrnBorder = new Border(mrnBox);
mrnBorder.Width = 1;
mrnBox.Border = mrnBorder;
mrnBox.Characteristics.Border = System.Drawing.Color.Black;
mrnBox.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
// Add form fields to first page of PDF document
pdfdoc.Form.Add(nameBox, 1);
pdfdoc.Form.Add(mrnBox, 1);
//Add Radiobuttons at specific position coordinates in PDF
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
//Set position here
table.Left = 200;
table.Top = 300;
table.ColumnWidths = "120";
page.Paragraphs.Add(table);
Aspose.Pdf.Row r1 = table.Rows.Add();
Aspose.Pdf.Row r2 = table.Rows.Add();
Aspose.Pdf.Cell c1 = r1.Cells.Add();
Aspose.Pdf.Cell c2 = r2.Cells.Add();
RadioButtonField rf = new RadioButtonField(page);
rf.PartialName = "radio";
pdfdoc.Form.Add(rf, 1);
RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();
opt1.OptionName = "Yes";
opt2.OptionName = "No";
opt1.Width = 15;
opt1.Height = 15;
opt2.Width = 15;
opt2.Height = 15;
rf.Add(opt1);
rf.Add(opt2);
opt1.Border = new Border(opt1);
opt1.Border.Width = 1;
opt1.Border.Style = BorderStyle.Solid;
opt1.Characteristics.Border = System.Drawing.Color.Black;
opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt1.Caption = new TextFragment("Yes");
opt2.Border = new Border(opt1);
opt2.Border.Width = 1;
opt2.Border.Style = BorderStyle.Solid;
opt2.Characteristics.Border = System.Drawing.Color.Black;
opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt2.Caption = new TextFragment("No");
c1.Paragraphs.Add(opt1);
c2.Paragraphs.Add(opt2);
pdfdoc.Save(dataDir + "Fillable_PDF_Form.pdf");

The screenshot below shows output PDF document containing form fields as specified in above code snippet:

PDF form

Fill, Modify, or Delete Form Fields in Existing PDF using C#

As we have explored creating a form in PDF document using C#, Aspose.PDF for .NET API supports working with existing PDF forms as well. Let us discuss the following features of the API.

i) Fill a Form Field in Existing PDF file using C# Programmatically

For filling the PDF forms, we will continue with the PDF document that was created in the example above. Below are the steps to fill fields in an existing PDF document:

  1. Load source PDF document
  2. Get Textbox Fields and fill values
  3. Get Radio button field and select an option from the group
  4. Save filled PDF form

Following code snippet follows these steps and explains how to fill fields in a PDF document using C#:

// Open document
Document pdfDocument = new Document(dataDir + "Fillable_PDF_Form.pdf");
// Get the fields
TextBoxField textBoxField1 = pdfDocument.Form["nameBox1"] as TextBoxField;
TextBoxField textBoxField2 = pdfDocument.Form["Box1"] as TextBoxField;
// Fill form fields' values
textBoxField1.Value = "A quick brown fox jumped over a lazy dog.";
textBoxField2.Value = "A quick brown fox jumped over a lazy dog.";
// Get radio button field
RadioButtonField radioField = pdfDocument.Form["radio"] as RadioButtonField;
// Specify the index of radio button from group
radioField.Selected = 1;
dataDir = dataDir + "Fill_PDF_Form_Field.pdf";
// Save updated document
pdfDocument.Save(dataDir);

Below screenshot shows filled form fields of PDF form using C#:

Fillable PDF

ii) Modify a Form Field in PDF Document using C#

Sometimes you may need to change value in any field of a PDF form. Changing a value in a form field is a basic use case that can be achieved with below steps:

  1. Load the PDF form
  2. Get a specific field using its name
  3. Modify field value
  4. Save the updated PDF document

Following code snippet shows how to change a value in a form field of PDF document:

// Open document
Document pdfDocument = new Document(dataDir + "Fill_PDF_Form_Field.pdf");
// Get a field
TextBoxField textBoxField = pdfDocument.Form["nameBox1"] as TextBoxField;
// Modify field value
textBoxField.Value = "Changed Value";
textBoxField.ReadOnly = true;
dataDir = dataDir + "ModifyFormField.pdf";
// Save updated document
pdfDocument.Save(dataDir);

It is noteworthy here that you can not only change the value of the form but other properties can be updated as well. For instance, the field has been marked as Read Only in the code snippet above.

iii) Delete a Form Field in Existing PDF File using C#

We have already learned about adding and filling PDF form fields. Now let us explore removing a form field. You need to follow the steps below:

  1. Load the PDF document
  2. Call Delete method with the name of the form field
  3. Save PDF Document

Following code snippet shows how to delete form field from PDF file using C#:

// Open document
Document pdfDocument = new Document(dataDir + "Fill_PDF_Form_Field.pdf");
// Delete a particular field by name
pdfDocument.Form.Delete("nameBox1");
dataDir = dataDir + "Delete_Form_Field.pdf";
// Save modified document
pdfDocument.Save(dataDir);

Preserve Extended Rights of PDF Forms using C#

A PDF form might have extended rights, also referred to as extended features, which you would want to preserve during form manipulation. You should save it incrementally while following the steps below:

  • Load the PDF document in Stream
  • Work with form
  • Save the file without any parameter

The following C# code snippet explains how to preserve extended rights of PDF form:

// Read the source PDF form with FileAccess of Read and Write.
// We need ReadWrite permission because after modification,
// We need to save the updated contents in same document/file.
FileStream fs = new FileStream(dataDir + "Fill_PDF_Form_Field.pdf", FileMode.Open, FileAccess.ReadWrite);
// Instantiate Document instance
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(fs);
// Get values from all fields
foreach (Field formField in pdfDocument.Form)
{
// If the fullname of field contains A1, perform the operation
if (formField.FullName.Contains("nameBox1"))
{
// Cast form field as TextBox
TextBoxField textBoxField = formField as TextBoxField;
// Modify field value
textBoxField.Value = "Preserve Extended Features";
}
}
// Save the updated document in save FileStream
pdfDocument.Save();
// Close the File Stream object
fs.Close();

Use JavaScript in PDF Form using C#

You can use JavaScript in PDF form fields with Aspose.PDF for .NET API. Let us follow the below steps to achieve this requirement:

  1. Initiate an instance of Document class
  2. Add a TextBoxField on the first page at specific page coordinates
  3. Set up JavaScript
  4. Specify document action
  5. Save PDF document

Following code snippet shows how to add JavaScript in PDF form using C#:

Aspose.Pdf.Document pdfdoc = new Aspose.Pdf.Document();
pdfdoc.Pages.Add();
Aspose.Pdf.Forms.TextBoxField textBoxField = new Aspose.Pdf.Forms.TextBoxField(pdfdoc.Pages[1], new Aspose.Pdf.Rectangle(85, 750, 215, 770));
textBoxField.PartialName = "textbox1";
textBoxField.Value = "Text Box";
//TextBoxField.Border = new Border();
Border border = new Border(textBoxField);
border.Width = 2;
border.Dash = new Dash(1, 1);
textBoxField.Border = border;
textBoxField.DefaultAppearance.FontSize = 10;
textBoxField.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
// Add field to the document
pdfdoc.Form.Add(textBoxField, 1);
string JS = @"var w = this.getField('" + textBoxField.PartialName + "'); var today = new Date(); w.value = today.toLocaleString();";
pdfdoc.OpenAction = new JavascriptAction(JS);
pdfdoc.Save(dataDir + "JS_Form.pdf");
view raw JS_Form.cs hosted with ❤ by GitHub

The JavaScript gets the current date and time of the system when PDF document is opened and that value is populated into a text box.

Conclusion

In this article, we have learned different aspects of working with form in PDF documents. Creating, filling or editing PDF forms is simple with Aspose.PDF for .NET API. The API offers many such exciting features to work with different files. Let us know your feedback or comments via Free Support Forum.

See Also