With the advent of computers and the internet, a lot of information is captured digitally. Different companies came up with solutions to make this process more efficient. One such solution is the fillable PDF forms. PDF forms are a popular choice that makes it easy to capture information digitally. PDF forms may be used for capturing survey data or as admission forms. In light of this, this article will teach you how to create, fill and edit fillable PDF forms using C++.
- C++ API for Creating, Filling, and Editing Fillable PDF Forms
- Create Fillable PDF Forms using C++
- Fill an Existing Form in a PDF File using C++
- Modify the Value of a Form Field in a PDF Form using C++
- Delete a Form Field from an Existing PDF Form using C++
C++ API for Creating, Filling, and Editing Fillable PDF Forms
Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. Furthermore, the API supports creating, filling, and editing fillable PDF forms. You can either install the API through NuGet or download it directly from the downloads section.
PM> Install-Package Aspose.PDF.Cpp
Create Fillable PDF Forms using C++
In this example, we will create a form from scratch with two text boxes and one radio button. However, one text box is multi-line, and the other is single line. The following are the steps to create a form in a PDF file.
- Create an instance of the Document class.
- Add a blank page to the document.
- Create an instance of the TextBoxField class.
- Set the properties of the TextBoxField like FontSize, Color, etc.
- Create an instance of the second TextBoxField and set its properties.
- Add both text boxes to the form using the Document->get_Form()->Add(System::SharedPtr<Field> field, int32_t pageNumber) method.
- Create a table.
- Create an instance of the RadioButtonField class.
- Add the RadioButton to the form using the Document->get_Form()->Add(System::SharedPtr<Field> field, int32_t pageNumber) method.
- Create two instances of the RadioButtonOptionField class to represent the options of the radio button.
- Set the OptionName, Width, and Height and add the options to the radio button using the RadioButtonField->Add(System::SharedPtr<RadioButtonOptionField> const & newItem) method.
- Add the options of the radio button to the cells of the table using the Cell->get_Paragraphs()->Add(System::SharedPtr<BaseParagraph> paragraph) method.
- Save the output file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to create a form in a PDF file using C++.
Fill an Existing Form in a PDF File using C++
In this example, we will use the file generated in the previous example. We will load the file using the Document class and fill its fields. The following are the steps to fill the fields of an existing PDF form.
- Load the PDF file using the Document class.
- Retrieve the TextBoxFields using the Document->get_Form()->idx_get(System::String name) method.
- Set the values of both the TextBoxFields using the TextBoxField->set_Value(System::String value) method.
- Retrieve the RadioButtonField using the Document->get_Form()->idx_get(System::String name) method.
- Set the value of the RadioButtonField using the RadioButtonField->set_Selected(int32_t value) method.
- Save the output file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to fill an existing form in a PDF file using C++.
Modify the Value of a Form Field in a PDF Form using C++
With Aspose.PDF for C++, we can also modify the value of a previously filled field. In this example, we will use the file generated in the previous example and modify the value of the first TextBoxField. To achieve this, please follow the steps given below.
- Load the PDF file using the Document class.
- Retrieve the TextBoxField using the Document->get_Form()->idx_get(System::String name) method.
- Update the value of the TextBoxField using the TextBoxField->set_Value(System::String value) method.
- Save the output file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to modify the value of a field in a PDF form using C++.
Delete a Form Field from an Existing PDF Form using C++
The API also allows you to delete form fields from existing PDF forms. The following are the steps to delete a form field from a PDF form.
- Load the PDF file using the Document class.
- Delete the field using the Document->get_Form()->Delete(System::String fieldName) method.
- Save the output file using the Document->Save(System::String outputFileName) method.
The following sample code shows how to delete a form field from an existing PDF form using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to create forms in PDF files using C++. Additionally, you have learned how to fill and modify existing fields in PDF forms. You have also seen how to remove a form field from a PDF form using Aspose.PDF for C++ API. The API provides a bunch of additional features for working with PDF files that you can explore in detail by going through the official documentation. If you have questions regarding any aspect of the API, please feel free to reach us at our free support forum.