Skip to main content
Participant
April 17, 2025
Question

Patial import of a PDF form using javascript

  • April 17, 2025
  • 4 replies
  • 395 views

Hi, 


I need some help on a javascript implementation. that will allow to import the data from fields from a previously filled PDF form. 

 

But... after hours trying to use opendoc in hidden ... my "target" document always remain "undefined" 

 

trying with oDoc ... same . 

 

i'm out of ideas... anyone here that would have succesfully implemented this kind of function ?

 

I am doing the below :

function importPDF(introFields) {

    try {
  var d = myTrustedBrowseForDoc();
  // console.println(d.toSource());
  console.println(d.cPath);

var oldfilepath = d.cPath;

  // open the documen as a hidden document
try {
var sourceDoc = app.openDoc({cPath:oldfilepath, bHidden:true});
}
    catch (e) {
      console.println("Error opending doc" + e);
    }

  // import the data in the intro section

// copy the data from oDoc to this


  for (var i=0; i<introFields.length; i++) {
    var theName = introFields[i];
    try {
      var oldValue = sourceDoc.getField(theName).value;
      this.getField(theName).value = oldValue;
    }
    catch (e) {
      console.println("ERROR on Import: " + theName + " - " + e);
    }
  }

oDoc.closeDoc();

} catch(e) {
    console.println("User cancelled Save As dialog box");}}

Any suggestion on what i'm doing wrong... is  welcome !

4 replies

Participant
April 25, 2025

THansk a LOt Guys for the guidance. 

I coudl make this work 🙂 

For reference sharing here the code added to the folder level:

myTrustedOpenDoc=app.trustedFunction(function (oDoc)
{
	app.beginPriv();
	var myTrustedSource = app.openDoc({cPath:oDoc, bHidden:true});
	app.endPriv();
	return myTrustedSource;
})


myTrustedImportPDF=app.trustedFunction(function importPDF(introFields) 
{

    try {
  var d = myTrustedBrowseForDoc();
  // console.println(d.toSource());
  console.println(d.cPath);

var oldfilepath = d.cPath;

  // open the documen as a hidden document
try {
	
var sourceDoc = myTrustedOpenDoc(oldfilepath);
}
    catch (e) {
      console.println("Error opending doc" + e);
    }

  // import the data in the intro section

// copy the data from oDoc to this

var oldFields = [];
	

  for (var i=0; i<introFields.length; i++) {
    var theName = introFields[i];
    try {
		var fieldObject = {};
		
		fieldObject.field = theName;
      var oldValue = sourceDoc.getField(theName).value;
      fieldObject.value = oldValue;
	  oldFields.push(fieldObject);
    }
    catch (e) {
      console.println("ERROR on Import: " + theName + " - " + e);
    }
  }

sourceDoc.closeDoc();

} catch(e) {
    console.println("User cancelled Save As dialog box");}
	
	return oldFields;
	
	})
try67
Community Expert
Community Expert
April 18, 2025

Try running it from the Console first. If it works, the issue is what @Thom Parker pointed out. If not, there are other issues at hand...

try67
Community Expert
Community Expert
April 18, 2025

PS. I would recommend you don't continue using the "this" variable after opening the file, as it could point to it. Keep a variable pointing to the old file before you open the new one, and use that, instead.

Something like this:

 

var targetDoc = this;

var sourceDoc = app.openDoc({cPath:oldfilepath, bHidden:true});

// from this point on use "targetDoc" instead of "this"

JR Boulay
Community Expert
Community Expert
April 18, 2025

[MOVED TO THE ACROBAT FORUM]

Acrobate du PDF, InDesigner et Photoshopographe
Thom Parker
Community Expert
Community Expert
April 17, 2025

The "app.openDoc" function only returns the document object when run from a priveleged context.

To fix this, your "importPDF"  function needs to be converted to a trusted function in a folder level script. 

This article explains it:

https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm

 

You can also find a number of tools for handling PDF data here:

https://www.pdfscripting.com/public/Form-Data-Handling.cfm#DataTools

https://www.pdfscripting.com/public/Automation-Tool-Descriptions.cfm

 

   

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often