Skip to main content

Document and Page Actions — PDF Document API for Java

  • 5 minutes to read

The PDF Document API allows you to specify actions that a PDF viewer runs when it opens a document, closes, saves, or prints the document, and opens or closes a page. You can also add document-level JavaScript that other actions in the document call.

The following table lists document and page actions:

Action API Supported Actions
Open action PdfDocument.setOpenAction(value) A page location or an interactive action
Document event actions (close, save, and print) PdfDocument.setActions(value) JavaScript actions
Document-level JavaScript PdfDocument.getJavaScripts() JavaScript actions
Page actions (open and close) Page.setActions(value) Interactive actions

Interactive actions are InteractiveAction descendants (for example, JavaScriptAction, NamedAction, GoToAction, and UriAction).

Specify the Open Action

The open action specifies the page location that a PDF viewer displays or the action that a PDF viewer runs when it opens the document. Pass an ActionTarget object to the PdfDocument.setOpenAction(value) method:

  • To display a page location, create the ActionTarget object with the ActionTarget(page, destination) constructor (for example, with a FitDestination object).
  • To run an action, create the ActionTarget object with the ActionTarget(action) constructor.

The following code snippet specifies an open action that displays the second page of a document and fits the page in the window:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Files.createDirectories(Path.of("output"));
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            // Open the document at the second page and fit the page in the window.
            Page secondPage = pdfDocument.getPages().get(1);
            pdfDocument.setOpenAction(new ActionTarget(secondPage, new FitDestination()));

            try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                pdfDocument.save(outputStream);
            }
        }
    }
}

Run JavaScript on Document Events

Create a DocumentActions object, specify JavaScript actions for document events, and pass the object to the PdfDocument.setActions(value) method. Call the following methods to specify actions:

Method Description
setClosing(JavaScriptAction value) Sets the JavaScript action that a PDF viewer runs before it closes the document.
setSaving(JavaScriptAction value) Sets the JavaScript action that a PDF viewer runs before it saves the document.
setSaved(JavaScriptAction value) Sets the JavaScript action that a PDF viewer runs after it saves the document.
setPrinting(JavaScriptAction value) Sets the JavaScript action that a PDF viewer runs before it prints the document.
setPrinted(JavaScriptAction value) Sets the JavaScript action that a PDF viewer runs after it prints the document.

The following code snippet specifies JavaScript actions that a PDF viewer runs before it prints and closes a document:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Files.createDirectories(Path.of("output"));
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            JavaScriptAction printingAction = new JavaScriptAction();
            printingAction.setJavaScript("app.alert('This document contains confidential information.');");

            JavaScriptAction closingAction = new JavaScriptAction();
            closingAction.setJavaScript("app.alert('Thank you for reviewing the document.');");

            DocumentActions documentActions = new DocumentActions();
            documentActions.setPrinting(printingAction);
            documentActions.setClosing(closingAction);
            pdfDocument.setActions(documentActions);

            try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                pdfDocument.save(outputStream);
            }
        }
    }
}

Add Document-Level JavaScript

A PDF viewer runs document-level JavaScript actions when it opens the document. Use these actions to define functions and variables that other JavaScript actions in the document call.

The PdfDocument.getJavaScripts() method returns a map of script names to JavaScript actions. Add, modify, or remove map entries to manage document-level JavaScript actions.

The following code snippet adds a document-level JavaScript function and calls the function from the document open action:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Files.createDirectories(Path.of("output"));
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            // Define a document-level JavaScript function.
            JavaScriptAction greetFunction = new JavaScriptAction();
            greetFunction.setJavaScript("function greet(name) { app.alert('Hello, ' + name + '!'); }");
            pdfDocument.getJavaScripts().put("Greeting", greetFunction);

            // Call the function when a PDF viewer opens the document.
            JavaScriptAction openAction = new JavaScriptAction();
            openAction.setJavaScript("greet('reader');");
            pdfDocument.setOpenAction(new ActionTarget(openAction));

            try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                pdfDocument.save(outputStream);
            }
        }
    }
}

Specify Page Actions

Create a PageActions object, specify actions for page events, and pass the object to the Page.setActions(value) method. Use the following methods to specify actions:

Method Description
setOpened(InteractiveAction value) Sets the action that a PDF viewer runs when it opens the page.
setClosed(InteractiveAction value) Sets the action that a PDF viewer runs when it closes the page.

The following code snippet specifies a JavaScript action that a PDF viewer runs when it opens the last page of a document:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Files.createDirectories(Path.of("output"));
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream)) {
            JavaScriptAction openedAction = new JavaScriptAction();
            openedAction.setJavaScript("app.alert('You reached the last page.');");

            PageActions pageActions = new PageActions();
            pageActions.setOpened(openedAction);

            Page lastPage = pdfDocument.getPages().getLast();
            lastPage.setActions(pageActions);

            try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                pdfDocument.save(outputStream);
            }
        }
    }
}

Remove Actions

Call the following methods to remove document and page actions:

Action Method Call
Open action PdfDocument.setOpenAction(null)
Document event actions PdfDocument.setActions(null)
Document-level JavaScript PdfDocument.getJavaScripts().clear()
Page actions Page.setActions(null) for each page

The following operations also remove JavaScript actions from a document:

Refer to the following help topics for additional information:

See Also