Skip to main content
Known Participant
July 20, 2017
Answered

I want to use a script to open a file and then add a watermark in one step

  • July 20, 2017
  • 2 replies
  • 1136 views

I want to use a script to open a file and then add a watermark in one step. I have a script that, when simplified, looks like this:

app.openDoc("filepath");

this.addWatermarkFromFile("filepath");

When I have acrobat opened to the home menu with no files open and run this script, it opens the file perfectly but doesn't add the watermark. It returns the error "this.addWatermarkFromFile is not a function". However, if i leave the document open after running it the first time and run it again it adds the watermark with no error. This rules out any filepath errors, since both lines work just not at the same time.

Here's the main question:

What is causing the "addWatermarkFromFile" function to fail the first time, but not the second? How do I fix it?

Adding a document level script to run upon opening the document would not be beneficial for what I'm trying to do here.

Thanks

This topic has been closed for replies.
Correct answer try67

You need to use the value returned by openDoc to get a reference to the Document object. However, unless you're running the code from the JS-Console or some other trusted context, this will require embedding a script in the file you're opening to "disclose" it. You can read about it in the documentation of openDoc.

2 replies

Known Participant
July 20, 2017

Thanks for the response try67. I will be using the JS-Console in acrobat so that is not an issue. Just to clarify, instead of using this.addWatermarkFromFile I would use the object that is returned from the first function instead of "this"? Also, what is the proper syntax for that? Sorry if these are silly questions, I am not a pro at JS.

try67
Community Expert
Community Expert
July 20, 2017

Correct. The syntax would be something like this (using your incomplete example above):

var doc = app.openDoc("filepath");

doc.addWatermarkFromFile("filepath");

Known Participant
July 21, 2017

Thanks! That worked perfectly.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 20, 2017

You need to use the value returned by openDoc to get a reference to the Document object. However, unless you're running the code from the JS-Console or some other trusted context, this will require embedding a script in the file you're opening to "disclose" it. You can read about it in the documentation of openDoc.