These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Import Txt file into a PDF textfield without user prompt

PJguy
Registered: Nov 6 2008
Posts: 5

I am trying to figure out how to import a text file into a PDF and set its contents to a text field.

I am using the following which works but prompts the user to choose a file. Is there any way to avoid having the user choose the file?

function contentLoad(){
this.importDataObject("MyData");
this.getField("myTextField").clearItems;
var oFile = this.getDataObjectContents("MyData");
var cFile = util.stringFromStream(oFile, "utf-8");
this.getField("myTextField").value = cFile;

}

In Page Properties>I added the function for Page Open

Thanks!!

My Product Information:
Acrobat Standard 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Does this script have to be run from the document? Could it be a Folder Level automation script? The importDataObject fucntion is one the most restricted of all the "import" functions. If you could put the code in a trusted function you'd be able to use it without user interaction.

If the code must be in the document, and you can control the format of the file you want to import, then you are better off using either "doc.importAnFDF()" or "doc.importAnXFDF()". All of these have the advantage that data is mapped to specific form fields, and with proper enabling they will work from reader. And, using the new Acrobat 9 cross domain enabling you should be able to avoid all the popup warnings.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

PJguy
Registered: Nov 6 2008
Posts: 5
thom,

thanks for the info. I want to keep the script running from the document so I can distribute to others. I can control the format so I tried your suggestion, importAnXFDF() but I'm getting an Invalid argument type on getDataObjectContents.

here is my code

function contentLoad(){

this.importAnXFDF("test.xfdf");
this.getField("myTextField").clearItems;
var oFile = this.getDataObjectContents("test.xfdf");
var cFile = util.stringFromStream(oFile, "utf-8");
this.getField("myTextField").value = cFile;
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The "importAnXFDF" imports the data directly into the associated fields. It doesn't have anything to do with the file attachemets. Of course the XFDF file has to be formatted correctly.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

PJguy
Registered: Nov 6 2008
Posts: 5
Thanks Thom, I did get this working. For anyone whos interested, the XFDF syntax below works. It does bring up a Security Warning box when tested on another computer, (havent tried with Acrobat 8 yet). The User has to click whether to Allow or Block. Thom, is there anyway around that?

thanks again!


<?xml version="1.0" encoding="UTF-8"?>12312 Main St.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
One of the new security features in Acrobat 9 is a trusted area. Files that are in the trusted area are given a pass on minor security threats like loading data and connecting to web resources. At least that's what I've been told. I haven't really given the feature a good going over. You can get to it in the preferences dialog. You also might want to look into Cross Domain files. This is a feature where the PDF though a special file, indicates the web locations that it trusts. I don't know if it will work for local files, but since Acrobat is complaining with the same error message that is does for web accesses, maybe it will work.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script