পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Sunday, January 22, 2017

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.
Sample Document 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;

public class AddJavaScript {

   public 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.
Adding javascript

No comments:

Post a Comment