- PDFBox - Home
- PDFBox - Overview
- PDFBox - Environment
- PDFBox - Creating a PDF Document
- PDFBox - Adding Pages
- PDFBox - Loading a Document
- PDFBox - Removing Pages
- PDFBox - Document Properties
- PDFBox - Adding Text
- PDFBox - Adding Multiple Lines
- PDFBox - Reading Text
- PDFBox - Inserting Image
- Encrypting a PDF Document
- JavaScript in PDF Document
- PDFBox - Splitting a PDF Document
- Merging Multiple PDF Documents
- PDFBox - Converting PDF To Image
- PDFBox - Adding Rectangles
PDFBox - Useful Resources
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 loadPDF() of the Loader class. This method accepts a RandomAccessReadBufferedFile object as a parameter, since this is a static method you can invoke it using class name as shown below.
// Loading an existing document
PDDocument document = Loader.loadPDF(
new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/sample.pdf"));
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 - Inserting JavaScript in a PDF Document
Suppose, we have a PDF document named sample.pdf, in the path D:/Projects/PDFBox/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 PDFBoxDemo.java.
package com.tutorialspoint.pdfbox;
import java.io.IOException;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
public class PDFBoxDemo {
public static void main(String args[]) throws IOException {
// Loading an existing document
PDDocument document = Loader.loadPDF(
new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/my_doc.pdf"));
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);
System.out.println("Data added to the given PDF");
//Saving the document
document.save("D:/Projects/PDFBox/PdfBox_Examples/new.pdf");
//Closing the document
document.close();
}
}
Output
Compile and run the code to verify the following output −
Data added to the given PDF
If you try to open the document new.pdf it will display an alert message as shown below.