Acrobat PDF files are one of those formats that are notoriously hard to automate. But, using a free script, you create a handy PDF form filler that you’ll learn about in this article!
If you’ve ever had to edit PDF form and fill out fields by hand, it can soon turn into a real pain. A PDF file isn’t plain text and requires specific software to read (and edit) them.
One way to fill out PDF form fields in an automated script is to use the .NET Framework. But to do so requires you to be a software developer. Luckily, there’s a .NET library called iTextSharp to help.
Prerequisites
For the instructions you’ll be receiving in this article to work, ensure you have:
- A current Windows PC (Windows 7 or Windows 10 will work)
- A beginner understanding of using the PowerShell scripting language
- A PDF field with fields to fill
Editing PDF Forms with a PowerShell Module
Since filling out PDF forms is not an easy task for non-programmers, I’ve created a PowerShell module called PDFForm. This module use the .NET library under the covers and makes working with it much easier.
If you don’t know how to import a module, check out this guide.
Downloading and Installing the PDF Tool
The first step is getting the PDFForm module on your computer. To do that:
- Copy the entire contents of the PDFForm PowerShell module, save it to a text file and call the text file PDFForm.psm1.
- Create a folder called PDFForm in the C:\Program Files\Windows PowerShell\Modules folder.
- Move the PDFForm.psm1 file to the PDFForm folder just created.
At this point, the PowerShell module should be available in your PowerShell console. To verify, open PowerShell and run Import-Module -Name PDFForm
. You should get no errors. If you do receive errors, something is amiss.
Finding the PDF Field Names
Once you’ve got the PDFForm module imported and a PDF file containing fields ready, you need to know the field names. To find these field names, run the Get-PdfFieldName
function. Provide the path to the PDF file and the path to the iTextSharp DLL file as shown below.
Get-PdfFieldNames -FilePath "C:\Vendor-Setup-Form.pdf" -ITextLibraryPath 'C:\users\Adam\dir\itextsharp.dll'
The path to the iTextLibrary DLL isn’t necessarily required, but you will need to use this if the auto-download of iTextLibrary does not work within the PDFForm module.
Filing in PDF Form Fields
You can see above that the PDF form example has quite a few fields available. Let’s now fill in a couple of them. Do this by using the Save-PdfField
function. Specify the following parameters:
Field
– This is a hashtable of each field name and value.InputPdfFilePath
– This is a file path that points to the PDF fileOutputPdfFilePath
– This designates where to create the filled-form PDF
$parameters = @{
Fields = @{'Name' = 'test123';'Bank Name' = 'some bank'}
InputPdfFilePath = "C:\Vendor-Setup-Form.pdf"
ITextSharpLibrary = 'C:\users\adam\dir\itextsharp.dll'
OutputPdfFilePath = 'C:\Users\Adam\test4.pdf'
}
PS> Save-PdfField @parameters
Now pull up the PDF file saved to OutputPdfFilePath
and you should see our PDF form filler has populated all fields in the new PDF.
Some PDF files contain fields other than text boxes which PDFForm supports. Unfortunately, other fields are not supported at this time. However, PDFForm is open source so if you extend its functionality, please contribute to the project and submit a pull request on Github to make it better for everyone!
Review the new PDF and check to see if it populated all of the fields you require. At that point, if the original PDF is no longer needed it can be deleted manually.
Summary
Use the PDFForm module next time you need to fill out a bunch of web forms. It will not only save you time but will prevent the most common cause of problems in the IT world; human error.