Skip to main content
Skip table of contents

How to create a JavaScript action to open the attachments panel?

Is there an action which opens the attachments panel? Not right away, but when the user presses some text?

Is there an action which opens the attachments panel? Not right away, but when the user presses some text?

I know the writer.setViewerPreferences(PdfWriter.PageModeUseAttachments) but I don't want it to open right away.

Posted on StackOverflow on Sep 12, 2012 by Michael A

This can be done using a JavaScript action. Use the following code in iText 7:

public void createPdf(String dest) throws IOException {
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    Document doc = new Document(pdfDoc);
    PdfAction action = PdfAction.createJavaScript("app.execMenuItem('ShowHideFileAttachment');");
    Link link = new Link("Show / Hide attachment panel", action);
    Paragraph paragraph = new Paragraph(link);
    doc.add(paragraph);
    doc.close();
}

Note that this won't work on all viewers (app = Adobe Reader) and it won't work if people disable Javascript.

Click How to create a JavaScript action to open the attachments panel? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.