Highlighted

Detect if PDF document is being opened in Reader and not in a browser tab

Participant ,
Jul 14, 2018

Copy link to clipboard

Copied

So, the title of my thread is very straight forward and that's what I am after... a piece of JS code that ensures me that the document IS in Reader.

The reason is because I have a set of PDF documents stuffed with a lot of JS and that just get stuck when the user's browser opens it in a tab instead to download it.

I kind of worked it around by adding the HTML5 property 'download' at my links declaration to force the download, however I know that not all browser versions recognizes it so once in a while I still will have someone staring to a broken document into a browser tab.

I would appreciate immensely if someone could tell me if it does exist such JS code that may help me with this detection.

Thanks!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

The way to do it is to display an error message by default, and then hide it when the script detects that the file is opened in Adobe software. That way, even if JS is not supported at all, the error message will still show up when the file is opened.

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

467

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

Detect if PDF document is being opened in Reader and not in a browser tab

Participant ,
Jul 14, 2018

Copy link to clipboard

Copied

So, the title of my thread is very straight forward and that's what I am after... a piece of JS code that ensures me that the document IS in Reader.

The reason is because I have a set of PDF documents stuffed with a lot of JS and that just get stuck when the user's browser opens it in a tab instead to download it.

I kind of worked it around by adding the HTML5 property 'download' at my links declaration to force the download, however I know that not all browser versions recognizes it so once in a while I still will have someone staring to a broken document into a browser tab.

I would appreciate immensely if someone could tell me if it does exist such JS code that may help me with this detection.

Thanks!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

The way to do it is to display an error message by default, and then hide it when the script detects that the file is opened in Adobe software. That way, even if JS is not supported at all, the error message will still show up when the file is opened.

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

468

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 14, 2018 0
Most Valuable Participant ,
Jul 14, 2018

Copy link to clipboard

Copied

Some browsers don't run any of your JavaScript... so how would a piece of code deal with this...?

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 14, 2018 0
Most Valuable Participant ,
Jul 14, 2018

Copy link to clipboard

Copied

The way to do it is to display an error message by default, and then hide it when the script detects that the file is opened in Adobe software. That way, even if JS is not supported at all, the error message will still show up when the file is opened.

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 14, 2018 1
Participant ,
Jul 14, 2018

Copy link to clipboard

Copied

Yup MVP this was exactly what I did after to stop and think a little while. Thank you anyway, I marked you answer as the right one!

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 14, 2018 0
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Hi,

 

how would I do that?

Are there any instructions? Because I do know how to add a javaScrip, but how would I add an error message by default? and then make the script make it dissapear?

 

Thanks!

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...
Sep 16, 2020 0
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Does anyone here know how I get the systen creation date of the file? what I am doing right now (this.info.CreationDate;) gets the creation date under Adobe-Properties and that seems to be a different date....

thanks!

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...
Sep 16, 2020 0
Participant ,
Jul 14, 2018

Copy link to clipboard

Copied

I already got the solution, thanks!

PS: Actually I didn't need any JS piece of code!

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 14, 2018 1
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

hi,

 

can you share it? I am having the same problem....

Thanks!

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...
Sep 16, 2020 0
Most Valuable Participant ,
Sep 16, 2020

Copy link to clipboard

Copied

The message to be displayed by default would be a large form field with a large message. You write the code to hide this if and only if in Reader. (This is a challenge, because some browsers run SOME JavaScript and you will need detailed experiments to find how to tell the real thing). Good luck!

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...
Sep 16, 2020 1
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

I think I got it!

the problem now is, that when I have the document open (in adobe) the form field disappears because my code runs because it is obiously open in adobe. Then when I upload the document on a sharepoint and open it in the browser it still is not visible because I think the code ran and made it dissappear when I had it open in adobe. 

 

I need to make the field visible again and then save, not open and upload to sharepoint. 

This is rather tedious but I guess there is no way around it?

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...
Sep 16, 2020 0
Participant ,
Sep 16, 2020

Copy link to clipboard

Copied

Cristina,

 

There is a little while since I did that so I can't recall exactly how to perform each step but the overall idea is quite simple. First you will need Adobe Acrobat Pro to edit your PDF document. I believe you already have it though since you mentioned that you know how to add JS to a PDF document. Said that let's go...

 

1) Open your PDF document with Adobe Acrobat Pro

2) Make layers

3) Add a new layer and name it "error"

4) Place into this layer an image or a text with your custom error message such as "You cannot view this document in browser. Please download it and open with Adobe Reader!"

5) Ensure that ALL layers are invisible and only this one is visible (only its checkbox should be checked and all others remain unchecked)

6) Open JS window. Click into 'Search tools...' box at the top right and type in "javascript". Then choose the option "All JavaScripts"

7) Into the window paste the following code:

 

var layers = this.getOCGs();

var aLayers= ["contents","error"];

for(var i=0;i<this.layers.length;i++)
   if (layers[i].name == "error")
      layers[i].state = false;
   else
      layers[i].state = true;

 

It should do the magic for you.

 

The trick here is that every time this document is opened (be in browser or in Adobe Reader) there WILL BE the message error. However when it be opened in Reader it will run the code that makes it invisible instanly so the user never will see it. By the other hand when it be opened in a browser tab, since browsers has not the ability to run PDF internal JS the message error will simply stay visible. Simple.

 

Good luck!

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...
Sep 16, 2020 1
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

actually, just solved that, I added a script when the file is saved to set the form field to appear again 🙂

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...
Sep 16, 2020 1
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

"... since browsers has not the ability to run PDF internal JS the message error will simply stay visible."

This is not correct!

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...
Sep 16, 2020 1
Participant ,
Sep 16, 2020

Copy link to clipboard

Copied

Really? Sure! LoL.

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...
Sep 16, 2020 0
Adobe Community Professional ,
Sep 16, 2020

Copy link to clipboard

Copied

E.g. Internet Explorer with Acrobat Reader add-on.

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...
Sep 16, 2020 1