I've been working with PowerShell for a long time, and it still amazes me the things that can be automated with it. One of these neat tricks that I've discovered is the ability to fill in PDF forms. At first, you might struggle to see where this would come in handy if you've ever had to fill out more than one form over and over again, you'd soon see the benefit here.

Since you can't fill in PDF form fields with PowerShell directly, you need to use .NET. Unfortunately, there's still no native way to do this in .NET but you can use a library called iTextSharp to get the job done. The iTextSharp library is a requirement for the script I'm about to show you.

The PDFForm PowerShell Module

Since this is not an easy task even with the iTextSharp library, I've created a PowerShell module called PDFForm that I'll be using. If you don't know how to import a module, check out this guide.

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, you can run the Get-PdfFieldName function in the PDFForm module. Provide the path to the PDF file and the path to the iTextSharp DLL file as shown below.

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.

PS> Get-PdfFieldNames -FilePath "C:\Vendor-Setup-Form.pdf" -ITextLibraryPath 'C:\users\Adam\dir\itextsharp.dll'
PDF field names

You can see that my PDF form 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 Field parameter using a hashtable of the field name and the value. Also, you'll need to specify the InputPdfFilePath parameter to designate the form you'd like filled in and the OutputPdfFilePath to select where to place the copy of the form.

To maintain the original for backup purposes, Save-PdfField creates a copy of the original and fills in the fields on that one.

$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

If you now pull up the C:\Users\Adam\test4.pdf file. you will see that each of the fields are populated 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.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!