PDF forms are frequently used to collect data and information. For example, Questionnaires are usually designed to collect responses for survey purposes. 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#
- Fill a Form Field in Existing PDF file using C#
- Modify a Form Field in PDF Document using C#
- Delete a Form Field in Existing PDF File using C#
- Preserve Extended Rights of PDF Forms using C#
- Use JavaScript in PDF Form using C#
Create Fillable PDF Forms using C#
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:
- Create an instance of Document class
- Add a blank page in PDF document
- Add TextBox Field in the form
- Set different properties of the fields including Font, Border etc.
- Add Radio Button in the form
- Save PDF document
Following code snippet shows how to create form in PDF using C#:
The screenshot below shows output PDF document containing form fields as specified in above code snippet:
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#
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:
- Load source PDF document
- Get Textbox Fields and fill values
- Get Radio button field and select an option from the group
- Save filled PDF form
Following code snippet follows these steps and explains how to fill fields in a PDF document using C#:
Below screenshot shows filled form fields of PDF form using C#:
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:
- Load the PDF form
- Get a specific field using its name
- Modify field value
- Save the updated PDF document
Following code snippet shows how to change a value in a form field of PDF document:
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:
- Load the PDF document
- Call Delete method with the name of the form field
- Save PDF Document
Following code snippet shows how to delete form field from PDF file using C#:
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:
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:
- Initiate an instance of Document class
- Add a TextBoxField on the first page at specific page coordinates
- Set up JavaScript
- Specify document action
- Save PDF document
Following code snippet shows how to add JavaScript in PDF form using C#:
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
Fill, Create, or Edit Fillable PDF Forms Programmatically using Java