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

how to open a pdf which be attached into a PDF using javascript?

pettysoldier
Registered: Feb 23 2011
Posts: 6
Answered

Hi guys,
 
My requirement is I designed a pdf form , and there is another pdf will attach into the pdf.I will add a button at the pdf form,when i click the button ,I want to open the attached pdf.
I use below javascript for this issue:
 
var oDoc = event.target;
var oAttachment = oDoc.dataObjects;
var oAttachmentName;
if (oAttachment != null) {
for (var i = 0; i < oAttachment.length; i++){
oAttachmentName = oAttachment[i].path;
if(oAttachmentName == "TEST.pdf"){
app.openDoc(oAttachmentName, this);
}
}
}
 
but app.openDoc just can open the external file,then I changed method,use app.openDataObject, and still not work.
 
Is there any way to resovle this issue????
 
thanks
 

My Product Information:
LiveCycle Designer, Windows
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
My, my,

this script looks familar ;-)

Is your attachment named "TEST.pdf" as described in my example?

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
"app.openDoc" requires the full path to the PDF. In this case you are trying to open an attachment. Which adds a twist to the whole thing.

In Acrobat version 8 and earlier, there were only two ways to open an embedded PDF so that it is visible to the user.
1. Use the Link tool to create to create a GoTo Destination to the embedded file
2. Launch the embedded file using the "doc.exportDataObject" function. Which will work for you here.

The "doc.openDataObject" only opens embedded files as "hidden". This is so they can be used as a data source. It cannot be used to display a PDF to the user. It would be nice if this was pointed out clearly in the documentation, but it isn't.

In Acrobat 9, Adobe snuck in a file path mechanism for embedded files. It's not well documented, but each embedded file has a path that can in fact be used with the "app.openDoc" function, or any other operation that requires a path. The path to the embedded file is prefixed with the path to the parent file, surrounded by pipe symbols. You can find the path for your file be doing this.

1. Open the main file in Acrobat 9 or later.
2. Open the embedded file the Attachements window
3. Open the JavaScript Console Window
4. Run this code: this.path

The displayed path can now be used to access the embedded file from a script.



Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

pettysoldier
Registered: Feb 23 2011
Posts: 6
Accepted Answer
to radzmar : yeah,this code is yours

thanks Thomp , use exportDataObject method works.


thank all of you guys!



charliegolf
Registered: Jan 29 2008
Posts: 5
Thom,
I know this thread is a bit on the old side, but I was hoping you might be able to help out on this issue. I'm using Acrobat 10. I have been using app.openDoc to link to PDFs but can't get it to work for attachments as you and radzmar have. I keep getting the NotAllowedError. What I've got:

var parentPath = event.target.path.toString(); //to get parent PDF path
var linkPath = xfa.resolveNode("Form.Page#.SubForm.#subform.targetPDF").rawValue //target PDF name
//open attachment
app.openDoc({cPath:"|" + parentPath + "|U:" + linkPath,oDoc: this});

When I console this.path after manually opening an attachment, I get a path like you described:
|/C/My Documents/Folder/SubFolder/FileName.pdf|U:AttachmentFileName.pdf
I put a textbox in the subform to check the constructed path and it shows the identical path (checked it three times).
I have made sure that all of my target PDFs are disclosed as well.

Is this a version 10 thing, or did I miss something?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Ok, I did a little research and found that the embedded path works in 10, but you have to get all the bits correct.

1. The file containing the target file must be opened first
2. The path to the target file must be correctly formed. Here's an example, run the following code from the console window, when a PDF containing an embedded file is already opened.

var cPath = "|" + this.path + "|U:\uFEFF" + this.dataObects[0].name;
app.openDoc(cPath);

The embedded file portion of the path is prefixed with the MultiByte character indicator, that's the "\uFEFF" bit. Notice also that the code uses the Name of the embedded object, not the file name. There's a difference. The code assumes that the first embedded file in the list is a PDF. If it's something else then the openDoc call will fail.




Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

PDFchild
Registered: Oct 31 2010
Posts: 44
Hello,

Can any one explain to me how to make this work at document level script, i.e. whenever I open the file, the attachment will also open at the same time.

I am a beginner, a simple working sample will greatly help me.

Many thanks

:)

PDFchild [at] gmail [dot] com - Go Green Save Paper Adopt a PDF

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The code I show in the last post will work, but its got a small spelling error use this code;

var cPath = "|" + this.path + "|U:\uFEFF" + this.dataObjects[0].name;
app.openDoc(cPath);

the "this.dataObjects" property is an array of all the embedded files.

When the document level scripts are executed the document environment is not fully initialized so this code may not work. So place it on the PageOpen event for the first page.


Another way to do this is to create a portfolio, instead of just using embedded files. The portfolio can be setup to display a specific embedded file when it is opened.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

cadorcl
Registered: Oct 7 2011
Posts: 2
Hello thomp,

I have tried both methods you have discribed but to no avail!


I am intending to make a PDF file that is comprised from one page of 5 headlines with 5 attachments

when I click on any headline, I want the corresponding attachment open at certain preassigned bookmark


I have tried the link method, but it leads to that the attachment open in the same window , so the main file lost


may you give some help to make that main PDF file

by the way, I have acrobat X

thanks in advance
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There is a preference on the "Documents" tab of the preferences dialog for setting where a document is opened. It's called "Open cross document links in same window". It's checked on by default. There is a companion JavaScript property "app.openInPlace" that can be set in a document level script to guarantee the action you want.

I believe the app.openDoc technique shown above will only operate in Acrobat 9 and 10.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

cadorcl
Registered: Oct 7 2011
Posts: 2
Thank you Thom, unchecking the box did the job

but when sharing or sending the file to other users, they may not have unchecked that box

so using an actionscript code become more reliable


so if you please, can you give the code required to open an attachment in seperate window to an assigned bookmark

and thank you again for your cooperation


thomp
Expert
Registered: Feb 15 2006
Posts: 4411
No, bookmarks won't work for this, but the code I provided two posts ago will.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

nilesh_gupta28
Registered: Oct 31 2011
Posts: 3
Hi Thom,

As you told in your comment that, this code will work only for PDF file.
Is there any way to open other file also?

I tried to use it for other types of document, it returns with error

NotAllowedError: Security settings prevent access to this property or method.
App.openDoc:21:Link:Page1:Annot1:Action1

I also tried to make openDoc fuction a trusted funtion still I got the same error,


Please help me on this.


Thanks,
Nilesh
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There are two different ways to open a non-pdf file attachment in Acrobat. One is to use the doc.exportDataObject() function with the "nLaunch" condition. This can be done with a script from within a PDF. The other method is to use the bUseConb parameter in the app.openDoc() function. But this cannot be done from a script within a PDF. It can only be done from a privileged context.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script