How to Open a Document Attached to a PDF File

Have you ever wanted to attach a document (e.g. a MS Word file) to a PDF document, and give the user the ability to launch that file with just a click on a button?

Usually, you have to save the attachment to a file, remember where you saved it, then go to that location and open the file using your Windows Explorer or the Finder on a Mac. With the solution I am about to present, that gets much easier to do for the user, but a bit more complex for the author of the PDF file.

Let’s assume we have two files, one PDF file named document.pdf and a MS Word document named attachment.docx – in the following you just have to replace your filenames with the ones that I am using.

I am using Acrobat DC Pro (running on a Mac) for the following instructions, this will work the same way (with slightly different tool names and a different user interface with older versions of Acrobat as well). You will need Adobe Acrobat – either Standard or Pro – for this, the free Reader is not able to create such documents.

Open your PDF document and go to the “Attachments” pane on the left side of the Acrobat user interface. The “Attachments” pane is represented by the paper clip icon:

2015 10 14 09 24 53

If you don’t see this pane, select the following menu to show it:

2015 10 14 09 26 24

Once the “Attachments” pane is displayed, click on the menu icon as indicated in the following screenshot, and select to add an attachment:

2015 10 14 09 27 37

Now navigate to the file you want to attach, select it and click “OK”. This should now show you the new attachment in the “Attachments” pane:

2015 10 14 09 32 37

So far we have not done anything special – this is just the process to attach a file to a PDF document. We could stop here and let the user figure out that there is actually an attachment in the PDF file, and how to open it. But that’s not what we set out to do, we want to make it obvious for the user how to display this attached document.

To do that, we need to add a button to the PDF document. There are two ways to do that: We can either open the form editor and then add a button (and deal with everything that comes with actually being in the form editor), or we can just add an interactive button. If this document is a form that contains other form fields, we of course would use the button function in the form editor, but if this is just one button we need to add, there is an easier way.

Just in case you are not yet familiar with the Acrobat DC user interface, it can be hard to find the one function you are looking for. That is why Adobe added a tool search function to Acrobat DC. There are two ways to search for tools: You can either use the search field at the top of the “Right Hand Pane” (or RHP for short), or you can select the “Tools” tab and then use the search field at the top of that dialog:

2015 10 14 09 37 05

2015 10 14 09 37 29

When we type in “Button” in that search field, Acrobat will tell us where the button tool is:

2015 10 14 09 40 26

This actually gives us two tools that we need: The tool to add a button, and the tool to select that button again and to modify it (if we need to make adjustments).

For now, just click on the “Add Button” search result. This dumps us right into the “Rich Media” toolset, with the Button tool selected. This means we can now place the button on the PDF page by moving it around to the correct location and then clicking to place it.

2015 10 14 09 42 28

At this time, the button tool is still selected, and we can double-click on the button to bring up it’s Properties dialog. This is where we need to make changes to give this button the ability to launch the attached Word document.

2015 10 14 09 45 42

Select the “Actions” tab (1), then select to create a “Mouse Up” action (2), select to run a JavaScript (3) and click on the “Add” button (4). This will bring up the JavaScript editor. Here we have to add a one line script.

This script will call the Doc.exportDataObject() method. You can find more information about this JavaScript method here: Acrobat JavaScript API – Doc.exportDataObject()

The trick here is to use the “nLaunch” parameter set to the value “2”, which has the following descrption:

  • If the value is 2, the file will be saved and then launched. Launching will prompt the user with a security alert warning if the file is not a PDF file. A temporary path is used, and the user will not be prompted for a save path. The temporary file that is created will be deleted by Acrobat upon application shutdown.

The command we are using also needs to reference the attachment name, which in our case is the filename we’ve originally imported:

this.exportDataObject({ cName: "attachment.docx", nLaunch: 2 });

Now close the editor by clicking “OK”.

A button is not very useful without a label (or an icon) on it. You can make these changes on the “Options” tab of the “Properties” dialog. After that, close the “Properties” dialog using the “Close” button and close the “Rich Media” toolset by clicking on the “x” on the right side of the toolbar.

When you now click on the button, you should see the following security dialog:

2015 10 14 10 06 17

This is it. You have a button that opens a Word document.

If you are trying to launch an attachment that is already in the PDF document, the filename you need to use for the JavaScript code is the name that is being displayed in the “Attachments” pane.

If you want to edit the button properties again, bring up the tool search function again, search for “button”, and now select the “Select Object” tool. The button should change it’s look, and you can now double-click on it to bring up the properties dialog again. Or, because now we know that the button is in the “Rich Media” toolset, we can go to the “Tools” tab and select this toolset directly:

2015 10 14 10 13 30

After the third time you’ve selected this tool, you probably wish that there would be an easier method of selecting the “Rich Media” toolset, and there is: You can drag&drop the toolset icon into the RHP:

2015 10 14 10 13 55

From now on, this toolset is just one button click away:

2015 10 14 10 14 21

You can download a PDF file with an embedded Word document form here: launchAttachment.pdf

This entry was posted in Acrobat, JavaScript, PDF, Programming, Tutorial and tagged , , , , , , . Bookmark the permalink.

31 Responses to How to Open a Document Attached to a PDF File

  1. Eric says:

    Hello,
    This is a great tutorial, but it doesn’t seem to be working when I try to recreate your steps. I’m using Acrobat 8 Professional and MS Word 2010.
    I saved your example PDF on my desktop and it works when I click the button; the Word doc launches fine. The problem occurs here:
    1. I save your Word attachment (attachment.docx) from your PDF onto my desktop.
    2. I delete your Word attachment (attachment.docx) from your PDF. There are now no attachments in your PDF.
    3. I attach the saved Word doc from my desktop (attachment.docx) to your PDF.

    At this point, when I click on the button the attached Word doc will NOT launch. No JavaScript has been changed and the Word doc and PDF have not been revised except for what I have detailed above. Do you have any suggestions?

  2. Karl Heinz Kremer says:

    Eric, I am sorry but I don’t have any suggestions. It should work. If you want to email the file to me, I can take a look. Please make sure that you explain in your email why you are sending the file – otherwise it will be impossible to match the file to this discussion.

  3. Veena Kambaty says:

    This is not working on ipad, do you have any solution so that this works on ipad. New window is not opening in Ipad.

  4. Karl Heinz Kremer says:

    Even, Unfortunately, no PDF viewer on any mobile device comes close to what you can do with the Adobe Acrobat or the free Adobe Reader. No, I don’t have a solution for you.

  5. R L Marshall says:

    Thanks for the tutorial. The steps worked for me. Can you provide a tip on how to make the attachment appear in a smaller pop-up window rather than as a a full-sized, separate document? I am making a PDF attachment open from within another PDF document, and I want it to appear in a smaller window, overlaid on top of the original document (i.e., a smaller, pop-up window reader). Any advice?

  6. Karl Heinz Kremer says:

    Rebecca, there is unfortunately no easy solution for your request. You can try to work with a hidden interactive button that gets only shown when necessary, and then use the page of your desired PDF file as the button image. Take a look at this function:

    http://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJS_API_AcroJS%2FJavaScript_API.htm%23TOC_buttonImportIconbc-689&rhtocid=_6_1_8_57_3

    This will require Adobe Acrobat (any version) or the free Adobe Reader XI or newer to work.

  7. Tom McCarty says:

    I am looking for a script I can use to make sure the file is only opened with an Adobe product.
    I have a file with a lot of script that only the free reader or writer from Adobe can run.
    Some of the users have other PDF readers that they like but that reader/writer can NOT run the adobe scripts I have created.
    Any help you have would be appreciated.
    Thanks,
    Tom

  8. Karl Heinz Kremer says:

    Tom, I can give you some general advice, but the actual solution is something I reserve for my customers. First, you cannot prevent anybody from opening your file. The file will open in e.g. the Chrome browser, with very limited form support. What you can do is make it obvious that they need to use a different PDF viewer. You can do that for example by replacing the first page, or placing something on the first page that alerts a user to that fact. You have to deal with situations where there is no JavaScript support and no form support, so your solution needs to be able to handle these cases as well. I use a combination of PDF layers and JavaScript to do that. If you want a full solution, please feel free to contact me via email. My email address is on the “About” page.

  9. Dave says:

    Nice one Karl

    Spent last 2 hours trawling the net for something like this so I can dynamically add a PDF to a newly created PDF, and link it to a button.

    Problem I had was that neither the name of the created or attached PDFs would constant, but would follow a convention of sorts

    Took 10 minutes to find that the java is case sensitive, and doesn’t like spaces immediately before the attachment extension.

    Got there in the end with this though.

    Thanks

  10. kajoh says:

    Hi, thanks for a great tutorial. I was wondering if you knew of a script modification to specify opening an Excel file attachment to the PDF at a specific worksheet, rather than just defaulting to the first worksheet?

  11. Karl Heinz Kremer says:

    kajoh, no, there is nothing to open a specific worksheet.

  12. Si says:

    Fabulous! Thanks for sharing!

  13. Mona Carol says:

    Thank you for this! This is exactly what I’ve been looking for – I knew there had to be a way to open an embedded file from within PDF that wasn’t another PDF. I really appreciate you taking the time to write this out and share.

  14. Valerie Lipow says:

    I just created a button and it works flawlessly to open an embedded file contained in the Attachments pane. Thank you!

  15. download download says:

    Klasse Webpage. Vielen Dank.

  16. Stefania says:

    It works, Thank you so much for posting!

  17. damian says:

    hola funciona perfecto, pero si quiero que abra en la pag 10 y no en la 1 como hago

  18. Karl Heinz Kremer says:

    Damian, unfortunately, that will not work. You can only open an attachment on the first page using this method. If the attached document is a PDF file, you can create a link to a page view, and then set that target view to the 10th page of your attached document. The general process is this:
    1) Attach the document to your PDF file
    2) Create a link to a page view in your first PDF file – this will show a dialog to set the link target
    3) Open the attachment by double-clicking on it
    4) Navigate to the correct target page
    5) Click on the appropriate button to set the link
    6) Close the attachment document
    7) Save the original PDF file

    The link is now set and when you click on it, it will open the target document on e.g. page 10.

    To automate this in a script is a bit more complex: You cannot execute a link form within JavaScript, but you can execute a bookmark – and you can create a bookmark that goes to a page view. So repeat the steps from above, but now within the context of creating a bookmark.

    Once that is done, you can use the Bookmark.execute() method to automate this. See here for more information: https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJS_API_AcroJS%2FBookmark_methods.htm%23TOC_executebc-2&rhtocid=_6_1_8_8_1_1

  19. YossiD says:

    Didn’t work for me in Acrobat 7 Pro. Attached an Excel file and entered the file name in the JavaScript but no dice. Here’s what I did:
    1. Opened the main PDF file.
    2. Used the paperclip tool to add and Excel (.xlsx) file as an attachment.
    3. Used the Button tool link tool to draw button somewhere in the PDF.
    4. I the Actions tab selected Mouse Up, Run a JavaScript > Add.
    5. In the JavaScript Editor I pasted: this.exportDataObject({ cName: “attachment.docx”, nLaunch: 2 }); and replaced the filename with the name of my attached file, and clicked OK, then Close.
    6. Selected the hand cursor and clicked the button.
    7. Nothing happens.

    Did I do something wrong?

  20. Karl Heinz Kremer says:

    Unfortunately, I don’t have access to Acrobat 7 anymore. It may just be too old to run this script.

  21. Andy Krantz says:

    Worked perfectly – great way to present several layouts to the boss. Thanks.

  22. Maggie says:

    Hello, it worked perfectly launching a video on a desktop but I tried on a mobile device and didn’t work, I read the comments and I saw the answer you gave to Veena Kambaty on 2015 about ipads, and I would like to ask if now, four years later, there are any way to make it work on mobile devices.

    Thank you in advance.

  23. Karl Heinz Kremer says:

    Maggie, the answer is still “No”.

  24. Alexander says:

    Hello,

    thank you for the great tutorial! I have one further question: is it now also possible to adress and further process the dataobject that was opened by using JavaScript?

    In my case the attached dataobject is a PDF form. Some form fields of this PDF Form can be populated with data from the first original PDF document (that contains the button) which is also a PDF form. I can now read the data from the form fields of the first document to variables and open the second document. But not populate the form fields of the second document with the data from the variables..

    thank you in advance!

  25. Karl Heinz Kremer says:

    Alexander, you can access the form fields in the attachment. Take a look at the doc.openDataObject() API function:

    https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJS_API_AcroJS%2FDoc_methods.htm%23TOC_openDataObjectbc-78&rhtocid=_6_1_8_23_1_77

    The documentation has some sample code that shows how to access form fields in the data object.

  26. Teresa Rocha says:

    Karl, hi! thank you for a great tutorial! Only one that made sense since I’ve started my research! It is driving me crazy that I can’t find a solution to my problem… I read that opening files through pdf doesn’t work on mobile devices BUT, I am trying to link a .vcf card inside a pdf document (the pdf file is a digital business card with all the other links – email, phone, social media pages, etc working fine). It makes total sense that once someone gets a digital business card (pdf file) on their mobile device, that he/she could save the info on their own cell phones/tablets, and the easiest way to do so is to save the .vcf file straight to their cell phones address book. So, do you have any suggestion on how to attach a .vcf file inside the pdf file, to work on mobile?

  27. Karl Heinz Kremer says:

    Teresa, PDF on mobile is a totally different environment than on the desktop, and a lot of things that work on the desktop will not work when you open a PDF on a mobile device. Just because we think that something is reasonable (and useful) does not necessarily mean that Adobe is thinking (and developing) along the same lines.

  28. Aaron says:

    Hi Karl – is there a workaround to open xlsm or xlsb files?

    Thanks!

  29. Karl Heinz Kremer says:

    Aaaron, if it does not work right out of the box, then unfortunately I am not aware of a workaround.

  30. Veery good article! We will be linking to this particularly gret post on our site.

    Keep up the great writing.

  31. Mike Nicol says:

    After hours trawling through Adobe help pages and assorted forums I stumbled upon this. Well written, non-technical, clear and concise, and above all else accurate. Thank you sir, I only wish I had found this sooner.

    Note: This doesn’t seem to work if the attached file has spaces in the name. Your example works fine, but real world files might need renaming, as mine did.

Leave a Reply

Your email address will not be published. Required fields are marked *