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

Community Beginner ,
Jul 20, 2017

Copy link to clipboard

Copied

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

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

198

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

Community Beginner ,
Jul 20, 2017

Copy link to clipboard

Copied

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

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

199

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 20, 2017 0
Most Valuable Participant ,
Jul 20, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 20, 2017 1
Community Beginner ,
Jul 20, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 20, 2017 0
Most Valuable Participant ,
Jul 20, 2017

Copy link to clipboard

Copied

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

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

doc.addWatermarkFromFile("filepath");

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 20, 2017 1
Community Beginner ,
Jul 21, 2017

Copy link to clipboard

Copied

Thanks! That worked perfectly.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 21, 2017 0