Fill fields in a PDF form

  • Ramón San Félix Ramón
  • Ramón San Félix Ramón's Avatar Code Author
  • Offline
3 months 5 days ago #531 by Ramón San Félix Ramón
Ramón San Félix Ramón created the code: Fill fields in a PDF form
Today I would like to share a small example that I made to be able to fill out Form 145 for my company's employees automatically.
(This model is used in Spain to declare family and personal data to the company to calculate personal income tax withholding on the payroll, that is, taxes)

Every year our advisor sends us this form so that we can give it to the employees and they can return it to us completed. To speed up this process, I decided to implement a method that would automate the filling out of the data, generating a personalized PDF for each employee.

The solution we present today is a simplified version of this process. Basically, it consists of a WebBrowser control that allows you to view the PDF of the form and two buttons that make it easier to obtain and write the fields.

The first button, called "Get Form Fields", has the function of detecting the fillable fields of the PDF. Clicking on it will display a Messagebox with the name of all identified fields. Additionally, to facilitate programming in the next button, this button copies a code snippet to the clipboard containing the names of the identified fields and the variables associated with their data as follows:

ls_FormFields[1] = "genter_nif"
ls_FormData[1] = ""

ls_FormFields[2] = "genter_compuesto"
ls_FormData[2] = ""

ls_FormFields[3] = "genter_anyo_nacimiento"
ls_FormData[3] = ""

This simplifies the process of manually filling out the fields programmatically.

The second button, called "Fill Form Fields", allows you to automate the process of filling out the fields. In the example provided, this button is programmed to complete the attached form "Modelo145.pdf", but you can easily modify the example to fill out any other form supported by the first button.

I want to emphasize that the attached form "Modelo145.pdf" is not an official template with fillable fields, but that I have created them personally with the names that I found convenient. However, this adaptation has been very useful to speed up work in my company. Remember that this example is just a simplified sample to illustrate the process of automating PDF form filling. If you decide to use it for your own forms, be sure to adjust the field names and associated data accordingly.

By the way, the .Net library that I used to create this example is iTextSharp( and I have compiled it in .Net6, I have tried to do the same with iText7 but without success, it gives me a strange error when using the library from PowerBuilder as with the digital signature example. Let's see if someone is encouraged and tries to solve it....

As always, I leave you the current project link on GitHub:

And project in Visual Studio 2022:

Attached here is the project compiled today in PowerBuilder 2022R3 Build 3356 along with the Visual Studio Project.

I always recommend going to the github links to find the latest version.

To be aware of what I publish you can follow my blog in Spanish:

This message has an attachment file.
Please log in or register to see it.

Please Log in or Create an account to join the conversation.

Moderators: Appeon Administrator