Skip to main content
January 14, 2026
Answered

Function app.openDoc not returning Doc object although run in privileged function

  • January 14, 2026
  • 3 replies
  • 274 views

Hello, 

I would very much appreciante your help. I have a privileged function stored on my local PC:

global.trustedSimpleFcn = app.trustedFunction(function(originalDoc) 
{

	app.beginPriv();
	var originalPath = originalDoc.path;

	// Get the path for the temp file
	var tempPath = app.getPath("user", "temp") + "/esc_temp_pdf.pdf";

	// This saves the original file as temp
	originalDoc.saveAs(tempPath);

	// Open original again
	app.openDoc(originalPath); 

	// Open the temp to get the doc object
	this.disclosed = true; // Because I heard it must be disclosed ??
	app.openDoc(originalPath); 
	var tempDoc = app.openDoc(tempPath);
	if (!tempDoc) {
	app.alert("tempDoc is empty!", 0, 0, "Error");
	app.endPriv();
	return "";
	}

	app.endPriv();
	app.alert("tempDoc is provided!", 0, 0, "Info");
	return tempDoc.documentFileName;
})

I am basically trying to save a copy of the current document as temp (and later manipulate it, flatten form and move it as a first page to other document, like a document header). For that I need to open a document and get the document object 'tempDoc' to be able to work with it.

 

 

I call this privileged function in button like this:

console.println(global.trustedSimpleFcn(this));

and tempDoc is empty. However if I run this one line of code in console, the tempDoc is provided ALTHOUGH this is run inside the privileged function. Why is there a difference? How can I fix it?

 

Regards,

Filip

Correct answer try67

Works fine for me. If it doesn't work on some machines for you it might be because of the use of the global object, which is not necessary, as was mentioned above. For it to work you must make sure that "Enable global object security policy" is not ticked under Preferences - JavaScript.

3 replies

try67
Community Expert
Community Expert
January 14, 2026

In addition to the correct suggestions given above, I don't see the need to open or close any files at all. Just use saveAs to save the file under a new path and then make your changes to it, using a reference the to Doc object (or even "this", which will continued to point to it). This won't affect the original copy of your file.

Thom Parker
Community Expert
Community Expert
January 14, 2026

To add a bit.  There is no reason to attach the trusted function to the global object. Since this is a folder level script, the function is already global.  

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
PDF Automation Station
Community Expert
Community Expert
January 14, 2026

It works for me both ways (button or console).  However, your script could be cleaned up a bit:

  • this.disclosed = true is not necessary in this context.
  • app.openDoc(originalPath) is repeated twice in a row.
  • Your second app.alert has no condition so it will activate whether tempDoc is provided or not.
  • Why is your second return outside of the privilege?
  • If you are dealing with 2 document objects and defining them both with variables, you should use those variables instead of this, to avoid confusion.
January 15, 2026

Thanks. Yea I thought the this.disclosed doesnt make much sence. About the other things, you are mostly right, but it is just an example code.

My problem is actually, that if I create a button and put the code there, then it has the neccessary rights or sth and actually runs also fine (meaning app.openDoc returns doc object). However if I open this file or other computer, having also the fcn ofc installed locally, it behaves differently (app.openDoc not returning doc object). The console behaves always the same.

This is actually my major issue.