How to Import text file or fdf file into pdf forms.

Hi,

I am using Mac OS X “Yosemite” - Version 10.10.5, Adobe Acrobat Pro XI - Version XI, Script Editor - Version 2.7.

Is there a way to import text file or fdf file into pdf forms using applescript?

Thanks
John

To create PDF files it is Acrobat Distiller you should use (Should be in your Acrobat folder). But i’m not sure you can create a pdf direct from a textfile.

Hi Fredrik,

Thanks for your reply.

I don’t want to create a PDF. I like to import text file or fdf file (Form Fields) into already created PDF.

Thanks,
John

You can use a command line utility named pdftk to get the job done. Word of warning, the version that is on the pdftk website is very old (2014) and just hangs a machine running a modern OS (10.13 and above). So you will need to download the version of pdftk that is available through MacPorts, and even that requires a special command to build successfully.

sudo port install pdftk +gcc42

But once you have it installed correctly, it runs like a dream.

I wish there was a way to do this with ASObjc, but I have no idea if that is possible.

I never saw a fdf file so I’m not sure that the script below - borrowed from Shane Stanley - may be used for this kind of file.

(* text from PDF *)

use AppleScript version "2.4"
use framework "Foundation"
use framework "Quartz"
use scripting additions

on getTextFromPDF:posixPath
	set theURL to current application's |NSURL|'s fileURLWithPath:posixPath
	set thePDF to current application's PDFDocument's alloc()'s initWithURL:theURL
	return (thePDF's |string|()) as text
end getTextFromPDF:
its getTextFromPDF:(POSIX path of (choose file of type {"pdf"}))

It may be time to try.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 26 novembre 2019 21:05:30

Hi Yvan,

So fdf stands for “forms data format”.

It is the data layer that stores the data for information filled in to a PDF form.

So you can extract this data from multiple filled out PDF forms for entering that info into a DB, or you can merge the FDF back into a PDF form. So, if you have 10 forms that you always have to fill out, and they contain a lot of shared information (Your Name for instance), you can use pdftk, and other commercial utilities to merge that data back into those 10 forms. So if you had to do that weekly, it would save you time. If you were a Dr. Office, with their plethora of forms to be filled out, you could save a HUGE amount of time automating the process of filling out those forms. I doubt that Quartz knows anything about that FDF layer, however. It would be great if it did!!!

To follow up on pdftk, I found that there is a newer version that is not listed on the pdftk website, here is the link to that newer version:

https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg

Hopefully it will work for whatever you are trying to accomplish!

I tried this utility. Unfortunately, it did not work for me. The fact is that a fdf file is always created based on a specific pdf file. Therefore, first you need to dump the pdf file objects in text form.

Then you can manually create the fdf file itself. This is also text, only text with a fdf-header and fields. And its structure should fully comply with the structure of the text dump, which, in turn, fully corresponds to the structure of the source, that is, the structure of the PDF.

That is, the PDF file in which you would like to write the field data of the fdf file may not be any PDF file, but only the one from which the fdf file itself was created. This is a vicious circle and it is not yet clear to me why this is necessary. Why not edit the contents of forms directly in PDF?

In any case, I tried to get a text dump using the pdftk utility, and everything ends for me already at this first step (getting text dump), giving an empty text…

STEP 1) Example to GET TEXT DUMP of PDF:

do shell script "usr/local/bin/pdftk path/to/the/form.pdf dump_data_fields > field_names.txt"

Here is example of text dump:

--
FieldType: Text
FieldName: first_name
FieldFlags: 0
FieldJustification: Left
---
FieldType: Text
FieldName: last_name
FieldFlags: 0
FieldJustification: Left
---
FieldType: Text
FieldName: occupation
FieldFlags: 0
FieldJustification: Center
---
FieldType: Button
FieldName: gender
FieldFlags: 0
FieldJustification: Center

STEP 2) Create FDF file, based on text dump (contents of “field_names.txt”) To do that, you add the fdf-header, fdf-fileds and fdf-footer.

It is text file like this


-- HEADER
%FDF-1.2
1 0 obj<</FDF<< /Fields
[
-- CONTENTS (FIELDS)
<< /T (first_name) /V (John)
<< /T (last_name) /V (Smith)
<< /T (occupation) /V (Teacher)>>
<< /T (age) /V (45)>>
<< /T (gender) /V (male)>>
-- FOOTER
] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF

STEP 3) HERE YOU EDIT FIELDS. IT IS EDITING TEXT, so it is easy.

STEP 4) RETURN EDITED DATA TO PDF. NOTE: you can’t edit original pdf(here is "form.pdf’), so you should create new (here is “output.pdf”), then move original PDF to trash, rename new pdf to name of original PDF:

do shell script "usr/local/bin/pdftk form.pdf fill_form $FDFfile output.pdf"

You should try
do shell script “usr/local/bin/pdftk path/to/the/form.pdf generate_fdf output save/path/for/FDFfile.fdf don’t_ask”

That will generate an FDF file that you can then edit and use to fill the PDF form.

So what I did was, on the original PDF, I put placeholder text like “PatientName” on the form where the patients name would go.
Then use the above command to generate the FDF form.
Then in the script I opened the fdf text file, parse it for “PatientName” and put the actual patient name where that placeholder text once was. Save that modified text back out to the text file, then you use the “fill_form” argument with PDFtk to fill the form back in again, creating a new PDF in the process.

Is it difficult? There is no need to use pdftk.

http://piyocast.com/as/archives/8357