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.

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

  1. Eric says:

    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:

    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.

  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.


  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.

Leave a Reply

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