Monday, December 9, 2019

#1 2019-03-26 11:14:02 am

johnmathew
Member
Registered: 2009-10-12
Posts: 94

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

Offline

 

#2 2019-03-27 03:57:44 am

FredrikM
Member
From:: Förslöv, Sweden
Registered: 2019-02-19
Posts: 21

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

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.

Offline

 

#3 2019-04-08 05:36:48 pm

johnmathew
Member
Registered: 2009-10-12
Posts: 94

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

Hi Fredrik,

Thanks for your reply.

To create PDF files it is Acrobat Distiller you should use (Should be in your Acrobat folder).



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

Offline

 

#4 2019-11-26 01:37:20 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 30

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

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.

Last edited by chrisndeca (2019-11-26 02:52:47 pm)

Offline

 

#5 2019-11-26 02:06:10 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3673

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

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.

Applescript:

(* 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

Offline

 

#6 2019-11-26 02:52:15 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 30

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

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!!!

Offline

 

#7 2019-11-27 03:53:04 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 30

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

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 … -setup.pkg

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

Offline

 

#8 2019-11-28 11:10:42 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 768

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

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:

Applescript:

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

Here is example of text dump:

Applescript:

--
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

Applescript:


-- 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:

Applescript:

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

Last edited by KniazidisR (2019-11-28 11:44:55 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#9 2019-12-04 05:03:58 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 30

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

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.

Last edited by chrisndeca (2019-12-04 05:06:24 pm)

Offline

 

#10 2019-12-04 07:41:30 pm

maro
Member
From:: Nerima, Tokyo, Japan
Registered: 2004-05-30
Posts: 41
Website

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

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

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


I wrote thousands of AppleScript to realize my idea. Natural language interface, voice recognition commander and so on. Though my mother toungue is strange language, Japanese, my most frequently write language is AppleScript. I believe it is for making things easy and powerful.


Filed under: PDF, form

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)