- PDFBox - Adding Rectangles
- PDFBox - Converting PDF To Image
- Merging Multiple PDF Documents
- PDFBox - Splitting a PDF Document
- JavaScript in PDF Document
- Encrypting a PDF Document
- PDFBox - Inserting Image
- PDFBox - Reading Text
- PDFBox - Adding Multiple Lines
- PDFBox - Adding Text
- PDFBox - Document Properties
- PDFBox - Removing Pages
- PDFBox - Loading a Document
- PDFBox - Adding Pages
- PDFBox - Creating a PDF Document
- PDFBox - Environment
- PDFBox - Overview
- PDFBox - Home
PDFBox Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PDFBox - JavaScript in PDF Document
In the previous chapter, we have learnt how to insert image into a PDF document. In this chapter, we will discuss how to add JavaScript to a PDF document.
Adding JavaScript to a PDF Document
You can add JavaScript actions to a PDF document using the PDActionJavaScript class. This represents a JavaScript action.
Following are the steps to add JavaScript actions to an existing PDF document.
Step 1: Loading an Existing PDF Document
Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below.
File file = new File("path of the document") PDDocument document = PDDocument.load(file);
Step 2: Creating the PDActionJavaScript Object
Instantiate the PDActionJavaScript object as shown below. To the constructor of this class, pass the required JavaScript in the form of String as shown below.
String javaScript = "app.alert( {cMsg: this is an example , nIcon: 3," + " nType: 0,cTitle: PDFBox Javascript example } );"; PDActionJavaScript PDAjavascript = new PDActionJavaScript(javaScript);
Step 3: Embedding Java script in the Document
Embed the required string to the PDF document as shown below.
document.getDocumentCatalog().setOpenAction(PDAjavascript);
Step 4: Saving the Document
After adding the required content save the PDF document using the save() method of the PDDocument class as shown in the following code block.
document.save("Path");
Step 5: Closing the Document
Finally, close the document using close() method of the PDDocument class as shown below.
document.close();
Example
Suppose, we have a PDF document named sample.pdf, in the path C:/PdfBox_Examples/ with empty pages as shown below.
This example demonstrates how to embed JavaScript in the above mentioned PDF document. Here, we will load the PDF document named sample.pdf and embed JavaScript in it. Save this code in a file with name AddJavaScript.java.
import java.io.File; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript; pubpc class AddJavaScript { pubpc static void main(String args[]) throws Exception { //Loading an existing file File file = new File("C:/PdfBox_Examples/new.pdf"); PDDocument document = PDDocument.load(file); String javaScript = "app.alert( {cMsg: this is an example , nIcon: 3," + " nType: 0, cTitle: PDFBox Javascript example’} );"; //Creating PDActionJavaScript object PDActionJavaScript PDAjavascript = new PDActionJavaScript(javaScript); //Embedding java script document.getDocumentCatalog().setOpenAction(PDAjavascript); //Saving the document document.save( new File("C:/PdfBox_Examples/new.pdf") ); System.out.println("Data added to the given PDF"); //Closing the document document.close(); } }
Compile and execute the saved Java file from the command prompt using the following commands.
javac AddJavaScript.java java AddJavaScript
Upon execution, the above program embeds JavaScript in the given PDF document displaying the following message.
Data added to the given PDF
If you try to open the document new.pdf it will display an alert message as shown below.
Advertisements