DocumentActions Class
Contains JavaScript actions that a PDF viewer runs on document close, save, and print events.
Declaration
public class DocumentActions extends TrackedObject
Remarks
Call the PdfDocument.getActions() method to get document actions. To specify document actions, create a DocumentActions object and pass it to the PdfDocument.setActions(value) method.
Call the following methods to specify actions for document events:
| 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. |
Refer to the following help topic for additional information: Document and Page Actions — PDF Document API for Java.
Example
Run JavaScript Before a PDF Viewer Prints or Closes a 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);
}
}
}
}
Inherited Members
Inheritance
DocumentActions()
Initializes a new instance of the DocumentActions class.
Declaration
public DocumentActions()
Methods
getClosing() Method
Returns the JavaScript action that a PDF viewer runs before it closes the document.
Declaration
public JavaScriptAction getClosing()
Returns
| Type | Description |
|---|---|
| JavaScriptAction | The JavaScript action, or |
getPrinted() Method
Returns the JavaScript action that a PDF viewer runs after it prints the document.
Declaration
public JavaScriptAction getPrinted()
Returns
| Type | Description |
|---|---|
| JavaScriptAction | The JavaScript action, or |
getPrinting() Method
Returns the JavaScript action that a PDF viewer runs before it prints the document.
Declaration
public JavaScriptAction getPrinting()
Returns
| Type | Description |
|---|---|
| JavaScriptAction | The JavaScript action, or |
getSaved() Method
Returns the JavaScript action that a PDF viewer runs after it saves the document.
Declaration
public JavaScriptAction getSaved()
Returns
| Type | Description |
|---|---|
| JavaScriptAction | The JavaScript action, or |
getSaving() Method
Returns the JavaScript action that a PDF viewer runs before it saves the document.
Declaration
public JavaScriptAction getSaving()
Returns
| Type | Description |
|---|---|
| JavaScriptAction | The JavaScript action, or |
setClosing(JavaScriptAction value) Method
Sets the JavaScript action that a PDF viewer runs before it closes the document.
Declaration
public void setClosing(JavaScriptAction value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | JavaScriptAction | The JavaScript action, or |
setPrinted(JavaScriptAction value) Method
Sets the JavaScript action that a PDF viewer runs after it prints the document.
Declaration
public void setPrinted(JavaScriptAction value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | JavaScriptAction | The JavaScript action, or |
setPrinting(JavaScriptAction value) Method
Sets the JavaScript action that a PDF viewer runs before it prints the document.
Declaration
public void setPrinting(JavaScriptAction value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | JavaScriptAction | The JavaScript action, or |
setSaved(JavaScriptAction value) Method
Sets the JavaScript action that a PDF viewer runs after it saves the document.
Declaration
public void setSaved(JavaScriptAction value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | JavaScriptAction | The JavaScript action, or |
setSaving(JavaScriptAction value) Method
Sets the JavaScript action that a PDF viewer runs before it saves the document.
Declaration
public void setSaving(JavaScriptAction value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | JavaScriptAction | The JavaScript action, or |