'Acrobat Form JavaScript works for Acrobat Pro DC but not Acrobat reader DC
I am trying to use the following document level JavaScript in an Acrobat form stored on SharePoint to tick a check box on the form depending on who the user is.
if (this.path === "/C/Users/" + getLoginName() + "/SharePointFolder/Test.pdf") {
if (getLoginName()==="Claire") {this.getField("Claire").value="Yes"}
if (getLoginName()==="claire") {this.getField("Claire").value="Yes"}
if (getLoginName()==="Lorna") {this.getField("Lorna").value="Yes"}
if (getLoginName()==="lorna") {this.getField("Lorna").value="Yes"}
}
This code works perfectly in Acrobat Pro DC but does not work in Acrobat Reader DC.
The following code works fine on both to put the users name in a text field, so I know the getLoginName() is working, but is not what i need:
this.getField("Username").value= getLoginName();
And the following code works fine in Acrobat Pro DC to prevent the original document from being overwritten but not in Acrobat Reader DC:
if (this.path == "/C/Users/" + getLoginName() + "/SharePointFolder/Test.pdf") {
var oDlg = {
description: { name: "NOT SAVED", elements: [
{ name: "NOT SAVED", type: "static_text", },
{ name: "Save to another location first", type: "static_text", },
{ type: "ok", },
] }
};
I am thinking that the syntax is slightly incorrect and that is why it is not working in Acrobat Reader DC.
Or is there a reason why Reader cannot use this.path ?
Any suggestions would be wonderful.
Thank you.
Edit:
Ok, I figured this out. I was using the wrong approach. I needed to find the user profile path and not the Windows username, as these can be different.
They just happened to be exactly the same on my Acrobat Pro machine.
It was this that was causing my code to fail.
The trusted function for finding the user profile path:
//Get and return the user's profile path name
var gUserPath = app.trustedFunction(
function () {
app.beginPriv();
return app.getPath("user");
app.EndPriv();
});
And my amended Document Javascript:
var UserPath = gUserPath();
var gUserPathArray = UserPath.split("/");
var gUserProfile = gUserPathArray[3];
if (this.path == "/C/Users/" + gUserProfile + "/SharePointFolder/Test.pdf") {
if (getLoginName()==="Claire" || getLoginName()==="claire") {this.getField("Claire").value="Yes"}
if (getLoginName()==="Lorna" || getLoginName()==="lorna") {this.getField("Lorna").value="Yes"}
I hope this helps others.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|