How to Open and Close an Attached PDF with Acrobat JavaScript
Question details
The user needs to execute a JavaScript workflow to open and close a PDF that is an attachment, which behaves differently than opening from a standard folder.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating PDF document management using Acrobat JavaScript, specifically handling attached PDFs.
- Observed behavior
- The JavaScript code works for normal folders but fails or behaves unexpectedly when the target PDF is an attachment.
Ensure you have the exact file name of the attached PDF and that your PDF viewer's security settings allow the execution of document-level JavaScript.
Use app.openDoc with the Current Document Reference
Modify your JavaScript code to explicitly reference the current document's container when opening the attached PDF.
When dealing with attached PDFs, the standard file path approach may not work because the attachment resides inside the parent PDF rather than a standard system folder. By passing 'this' as the second parameter to app.openDoc, you instruct the application to look within the container of the currently active document.
In your JavaScript code, use the syntax app.openDoc("DocToClose.pdf", this); to open the attached file.
Assign the result of app.openDoc to a variable (e.g., var attachedDoc = app.openDoc(...)) so you can reference the newly opened document object later.
Use the captured document reference to call the closeDoc() method, ensuring you close the intended attachment rather than the parent document.
Review your script and remove any unused variables or unnecessary array declarations that might have been left over from copied code to prevent memory leaks or logic errors.
Manage PDFs Easily with WPS Office
If you are looking for a lightweight and cost-effective way to view, edit, and manage PDF files and attachments without complex scripting, WPS Office provides a comprehensive and user-friendly PDF toolkit.
- 1. Download and Install: Get WPS Office from the official website and install it on your device.
- 2. Open your PDF: Launch WPS PDF and open your main document containing the attachments.
- 3. Access Attachments: Click on the Attachment icon (paperclip) in the left sidebar to view, open, or save attached files directly.

Frequently Asked Questions
Why does my JavaScript work for local folders but not for PDF attachments?
PDF attachments are stored within the data container of the parent PDF, not as standalone files in a standard operating system directory. You must use specific API parameters, like passing 'this' to app.openDoc, to reference the parent document's internal structure.
How do I properly close a PDF document using Acrobat JavaScript?
You need to call the closeDoc() method on the specific document object. Ensure you capture the document reference when opening it (e.g., var myDoc = app.openDoc(...)) so you can later execute myDoc.closeDoc() safely.
Can I extract a PDF attachment without using JavaScript?
Yes, most modern PDF readers, including WPS PDF and Adobe Acrobat, have a built-in attachments panel (usually represented by a paperclip icon) where you can manually open, extract, or save attached files to your local drive.




