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

Automate Creation of multiple PDFs from multiple XFDF files

jwhitlinger
Registered: Jan 6 2010
Posts: 5

I'm attempting to automatically create multiple PDF files with pre-populated form fields based on data from an Excel workbook. From various posts in these forums, I've successfully:
1. Created a PDF Form with all of the desired fields using the Acrobat Forms Tools (not Designer).
2. Created multiple XFDF files from Excel that can be imported manually using the "Form Data\Import Data to Form" menu options. I'm also able to just double-click the xfdf files from File Explorer and populate the PDF Form.

What I'd like to do next is automate the Open, Import, Save process since I have hundreds of files to create. I've attempted to use a javascript in batch processing (this.importAnXFDF), but it doesn't seem to want to function on xfdf files directly.

Am I on the right track? Is there a better way to create pre-populated PDF files from an external data source in an automated fashion?

Appreciate any help/suggestions. This forum is excellent.

My Product Information:
Acrobat Pro 7.1.4, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, there is a much better way to do this. For one thing, a batch process is really the wrong way to go. Batch operates on a group of PDF files. And you want one PDF file to load several different data sets and generate several different files.

Acrobat will import data directly from an Excel spreadsheet using the ADBC object or from a tab delimited file that can be exported from Excel. Here's a thread that covers this exact issue.

http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=23596

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/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

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

jwhitlinger
Registered: Jan 6 2010
Posts: 5
Hi Thom,
Thank you very much for all the information you have posted on this forum. Here's what I pulled together. Feel free to suggest better ways of doing this.

Here's a summary of what I did.
1. Created two trusted functions in config.js so the restricted functions could be called from a form button.
2. Created a button on the master form with the following javascript code. Basically, it loops thru the text file importing the data for each row and then saving a separate pdf file based on one of the imported fields (employee name).

try {
var FileToImport = myTrustedFileBrowser();
} catch(e) {
console.println("Unable to call file browser. Check config.js...");
console.println("error is: " + e);
}if (FileToImport != "undefined")
{
// Following code will loop thru a tab delimited text file and create a PDF for each row
var ExitFlag = false;
var WhichRow = 0;
var aPath = this.path.split("/"); // Split the file path into an array
aPath.pop(); // Remove the last element (which is the file name)
var cFilePath = aPath.join("/") + "/" // Put it back together without the filename
for (var WhichRow = 0; ExitFlag == false; WhichRow++)
{
rc = this.importTextData(FileToImport, WhichRow);
if (rc == 0)
{
var f = this.getField("EmployeeName");
this.removeField("cmdImportText"); // Remove the import button from the mass generated PDFs
try {
myTrustedSaveAs(this, { cPath: cFilePath + f.value + ".pdf", cFS:this.cFS });
} catch(e) {
console.println("Save not allowed, perhaps readonly.");
console.println("error is: " + e);
}
} else
{
ExitFlag = true;
};
}
}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Hey, that's pretty good. Look like you've hit all the important bits and the code is compact. I can't see what you are using to browse for the ".txt" file but I'm assuming it must be the "browseForFileToSubmit()" function?

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/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

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

jwhitlinger
Registered: Jan 6 2010
Posts: 5
Thom,
You are correct on browseForFileToSubmit. I've had to make one other change since my last post to support a change in security on the importTextData method for Acrobat 8.

Thanks again for all your posts on these forums. I wouldn't have been able to do this without them!

Here are the trusted functions from the config.js file:

myTrustedSaveAs = app.trustedFunction( function ( doc, oArgs )
{
app.beginPriv();
var myTrustedRetn = doc.saveAs( oArgs );
app.endPriv();
return myTrustedRetn;
});myTrustedFileBrowser = app.trustedFunction( function ()
{
app.beginPriv();
var tmpDoc = app.newDoc(0,0);
var fld = tmpDoc.addField("tmpTxt","text",0,[0,0,0,0]);

fld.fileSelect = true;

fld.browseForFileToSubmit();

var filePath = fld.value;

tmpDoc.closeDoc(true);

app.endPriv();
return filePath;
});myTrustedImportTextData = app.trustedFunction( function (doc, FileToImport, WhichRow)
{
app.beginPriv();
rc = doc.importTextData(FileToImport, WhichRow);
app.endPriv();
return rc;
});