見出し画像

Filling out PDF Forms with PDFtk

In this guide, I'll introduce an easy method for filling out PDF forms using PDFtk.


What is PDFtk?

PDFtk (short for PDF Toolkit) is a toolkit for manipulating Portable Document Format (PDF) documents.[3][4] It runs on Linux, Windows and MacOS.[5] It comes in three versions: PDFtk Server (open-source command-line tool), PDFtk Free (freeware) and PDFtk Pro (proprietary paid).[2] It is able to concatenate, shuffle, split and rotate PDF files. It can also show and update metadata. Both CLI and GUI versions of PDFTK are available.

https://en.wikipedia.org/wiki/PDFtk

Installing PDFtk

First, let’s download PDFtk Free from the website below and install it on your PC.

PDFtk is typically installed below:

C:\Program Files (x86)\PDFtk\bin\pdftk.exe

Getting Field Information

After installation, let's start using PDFtk. We'll perform all the steps via the command prompt. For demonstration, I've prepared a simple PDF file with three text fields: "Name", "Age", and "Mail" (located at C:\Test\PDF\SampleField.pdf ).

To input data, we need to know the field names. We can retrieve this information using the following command:

"C:\Program Files (x86)\PDFtk\bin\pdftk.exe" "C:\Test\PDF\SampleField.pdf" dump_data_fields_utf8 output "C:\Test\PDF\dump.txt"

The resulting dump.txt file will contain the field names:

  • Name

  • Age

  • Mail

---
FieldType: Text
FieldName: Name
FieldFlags: 0
FieldJustification: Left
---
FieldType: Text
FieldName: Age
FieldFlags: 0
FieldJustification: Left
---
FieldType: Text
FieldName: Mail
FieldFlags: 0
FieldJustification: Left

dump.txt

Inputting Data into Fields

Next, let's prepare an input data file. We'll use an XML file in XFDF format (located at C:\Test\PDF\SampleField_data.xfdf , encoded in UTF-8):

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
  <fields>
    <field name="Name">
      <value>foo bar</value>
    </field>
    <field name="Age">
      <value>18</value>
    </field>
    <field name="Mail">
      <value>foo@***.com</value>
    </field>
  </fields>
</xfdf>

Now, let's input the data into the fields using the following command:

"C:\Program Files (x86)\PDFtk\bin\pdftk.exe" "C:\Test\PDF\SampleField.pdf" fill_form "C:\Test\PDF\SampleField_data.xfdf" output "C:\Test\PDF\output.pdf" need_appearances

The resulting output.pdf will contain the filled-in data. You can open it and verify that the values have been successfully entered into the PDF form.


Remember that while RPA tools like Power Automate for desktop can also automate data input into PDF forms, PDFtk provides a stable and straightforward solution for this task.

いいなと思ったら応援しよう!