Pdfbox 简明教程

PDFBox - JavaScript in PDF Document

在上一章中,我们学习了如何将图像插入 PDF 文档。在本章中,我们将讨论如何将 JavaScript 添加到 PDF 文档中。

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

可以使用 PDActionJavaScript 类将 JavaScript 操作添加到 PDF 文档。这表示一个 JavaScript 操作。

You can add JavaScript actions to a PDF document using the PDActionJavaScript class. This represents a JavaScript action.

以下是向现有 PDF 文档添加 JavaScript 操作的步骤。

Following are the steps to add JavaScript actions to an existing PDF document.

Step 1: Loading an Existing PDF Document

使用 PDDocument 类的静态方法 load() 加载现有 PDF 文档。此方法接受一个文件对象作为参数,因为这是一个静态方法,您可使用类名调用它,如下所示:

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

如下所示,实例化 PDActionJavaScript 对象。为此类的构造函数传递所需的 JavaScript,形式为 String,如下所示。

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

将所需字符串嵌入 PDF 文档,如下所示。

Embed the required string to the PDF document as shown below.

document.getDocumentCatalog().setOpenAction(PDAjavascript);

Step 4: Saving the Document

添加所需内容后,使用 PDDocument 类的 save() 方法保存 PDF 文档,如下面的代码块所示。

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

最后,使用 PDDocument 类的 close() 方法关闭文档,如下所示。

Finally, close the document using close() method of the PDDocument class as shown below.

document.close();

Example

假设我们有一个名为 sample.pdf 的 PDF 文档,路径为 C:/PdfBox_Examples/ ,并且页面为空,如下所示。

Suppose, we have a PDF document named sample.pdf, in the path C:/PdfBox_Examples/ with empty pages as shown below.

sample document

此示例演示如何将 JavaScript 嵌入上述 PDF 文档。此处,我们将加载名为 sample.pdf 的 PDF 文档,并在其中嵌入 JavaScript。将此代码保存到名为 AddJavaScript.java. 的文件中。

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();

   }
}

使用以下命令从命令提示符处编译并执行已保存的 Java 文件。

Compile and execute the saved Java file from the command prompt using the following commands.

javac AddJavaScript.java
java AddJavaScript

执行后,上述程序将 JavaScript 嵌入给定 PDF 文档,显示以下消息。

Upon execution, the above program embeds JavaScript in the given PDF document displaying the following message.

Data added to the given PDF

如果您尝试打开文档 new.pdf ,它会显示如下所示的警报消息。

If you try to open the document new.pdf it will display an alert message as shown below.

adding javascript