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

Customized message based on where document originated

felidae990
Registered: Dec 2 2008
Posts: 12
Answered

We have a bunch of Acrobat files in our SharePoint document libraries that contain procedures everybody on site is supposed to follow, and since many of them are constantly under revision, people are supposed to refer to the most current revision posted online.
 
Unfortunately, sometimes people save these Acrobat files to their hard drives and end up using a document that's out of compliance because it's no longer current. To prevent that, I embedded a JavaScript in all these files that adds a date stamp and the current file path to every document they print so you can see when and where it originated.
 
I thought that would be sufficient, but my boss wants me to change the JavaScript to first check to see if the file path originates from SharePoint, and if not, she wants it to create a watermark stamped "Uncontrolled Copy" on every page, but a lot of people on site only have Acrobat Reader which I understand can't create watermarks from JavaScripts.
 
Is there an example in the forum of a JavaScript that stamps a custom message based on where the file originated?

My Product Information:
Acrobat Pro Extended 9.4, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can use 'this.path' or 'this.URL' and get the full path including the name of the PDF. You can then use the 'split()' method to get the parts into an array of the path and then either build up a string for your sharepoint server or test each level to verify if the PDF is located on your sharepoint site.

George Kaiser

felidae990
Registered: Dec 2 2008
Posts: 12
Could you show me how to do this in an If/Then JavaScript statement? For example, my script is
v1 = this.URL.substring(0,4); if (v1 = "http") this.getField('MyDate').value = "Printed on " + util.printd("mm-dd-yy, HH:MM:ss",new Date()) + "."; if (v1 = "file") this.getField('MyDate').value = "This is an Uncontrolled Copy."
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You are describing two issues:
1. Identifying where the file originates from.
2. Adding a watermark in Reader.

1. As George said, you can use this.path or this.URL for that. Here's an example:
Let's say your SharePoint files are located on the U drive. You can then use this code to identify them:

if (this.path.substring(0,3)=="/U/"){
// This file is from the SharePoint drive
} else {
// This file is located somewhere else
app.alert("You should only access this file from the SharePoint drive!");
}

2. As you said, you can't use the methods to add watermarks in Reader. But there are other options.
For example, you can create the watermark in Acrobat, hide it, and then display it in Reader (it's an OCG object).
Or you can use form fields to do the same.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

felidae990
Registered: Dec 2 2008
Posts: 12
I followed the example you gave me, and wrote this script for Document Actions -> Document Will Print.if (this.path.substring(0,4)=="http"){
this.getField('MyDate').value = "Printed on " + util.printd("mm-dd-yy, HH:MM:ss",new Date()) + ".";
} else {
this.getField('MyDate').value = "This is an Uncontrolled Copy."
}

The above example did work. When I printed it from my hard drive, it was stamped "Uncontrolled Copy," and when I printed it from SharePoint, it was stamped with the current date.

But since I have to add this script to over ten thousand documents, I need to create a batch script to add it to the Document Actions on ALL these files, yet when I attempt to add the "{" character to a batch script to set the script triggers, Acrobat tells me it's an illegal character. Can you show me the proper way to set the Document Actions in a batch file? Below is the portion of the script that fails due to the misplaced bracket.

// If this document resides on the web, stamp
// the current date on every page, else stamp it
// with the warning that it's an Uncontrolled Copy.
if (this.path.substring(0,4)=="http"){
this.getField('MyDate').value = \ "Printed on \" + util.printd("mm-dd-yy, HH:MM:ss",new Date()) + \".\";
} else {
this.getField('MyDate').value = \ "This is an Uncontrolled Copy.\"");
}

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Have a look at the documentation for doc.setAction()
Keep in mind that all the quotes have to be escaped when you place them in a string. Brackets don't need to be escaped, but you do need to add a back-slash at the end of a line.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

felidae990
Registered: Dec 2 2008
Posts: 12
Ok, after using the setAction command, my batch script will successfully set the script triggers until I add the "{}" brackets. Even if I include the back slashes at the end of the lines where they appear, Acrobat still tells me that they're illegal characters.

// If this document resides on the web, stamp
// the current date on every page, else stamp it
// with the warning that it's an Uncontrolled Copy.
if (this.path.substring(0,4)=="http"){\
this.setAction("WillPrint", "var today = new Date(); this.getField('MyDate').value = \"~DCRM~ Printed from controlled source on \" + util.printd(\"mm-dd-yy, HH:MM:ss\",new Date()) + \" hours.\"");
} else {\
this.setAction("WillPrint", "var today = new Date(); this.getField('MyDate').value = \"This is an Uncontrolled Copy.\"");
}\

try67
Expert
Registered: Oct 30 2008
Posts: 2398
You didn't escape any of the double-quotes. Also, don't add a back-slash to the last line. If you still have problems, post your entire code.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

felidae990
Registered: Dec 2 2008
Posts: 12
Well . . . when I type "escape quotes" in the search engine for this community forum, ALL the examples I've seen show a backslash. In fact, my original scripts used the backslash to enclose the quotation marks before I attempted to add my If-Then statement. Is there more than one character used to escape quotes?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
No, I see now that you did that part ok. But the backslashes at the end of the lines are only necessary inside the quoted script.

Also, you have a logical error. The check whether the file's path starts with "http" needs to be a part of the script you're embedding. Otherwise it will only work at the moment you're running the batch. You want it to make this check at the moment the user tries to print the file.

This is the code you want to use:

var script = "if (this.path.substring(0,4)==\"http\"){\
var today = new Date(); \
this.getField('MyDate').value = \"~DCRM~ Printed from controlled source on \" + util.printd(\"mm-dd-yy, HH:MM:ss\",new Date()) + \" hours.\";\
} else {\
var today = new Date(); \
this.getField('MyDate').value = \"This is an Uncontrolled Copy.\";\
}";

this.setAction("WillPrint", script);

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

felidae990
Registered: Dec 2 2008
Posts: 12
Sorry for the late response, but it worked perfectly. Thank you so much!