14.向PDF文档添加JavaScript(JAVA+PDFBOX)

[翻译]上一章中,我们已经学习了如何向PDF文档插入图片。在本章中,我们将讨论如何向PDF文档添加JavaScript。]

[原文]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 向PDF文档添加JavaScript

[翻译]
您可以使用
PDActionJavaScript类向PDF文档添加JavaScript动作。这个类表示一个JavaScript动作。

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

  • JavaScript /'daevskrpt/ JavaScript(脚本语言)
  • action /'aekn/ 动作
  • represent /repr'zent/ 表示

[翻译]
以下是向现有PDF文档添加JavaScript动作的步骤。

[原文]
Following are the steps to add JavaScript actions to an existing PDF document.

  • following /'fɑlo/ 以下的
  • step /step/ 步骤

Step 1: Loading an Existing PDF Document 步骤1:加载现有PDF文档

[翻译]
使用
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);
  • load /lod/ 加载
  • static /'staetk/ 静态的
  • parameter /p'raemtr/ 参数
  • invoke /n'vok/ 调用

Step 2: Creating the PDActionJavaScript Object 步骤2:创建PDActionJavaScript对象

[翻译]
按照以下方式实例化
PDActionJavaScript对象。向该类的构造函数传递所需的JavaScript代码,以字符串形式提供,如下所示。

[原文]
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);
  • instantiate /n'staeniet/ 实例化
  • constructor /kn'strktr/ 构造函数
  • required /r'kward/ 所需的
  • string /str/ 字符串

Step 3: Embedding JavaScript in the Document 步骤3:在文档中嵌入JavaScript

[翻译]
按照以下方式将所需的字符串嵌入到PDF文档中。

  • 将JavaScript动作设置为文档目录的打开动作

[原文]
Embed the required string to the PDF document as shown below.

document.getDocumentCatalog().setOpenAction(PDAjavascript);
  • embed /m'bed/ 嵌入
  • document /'dɑkjumnt/ 文档

Step 4: Saving the Document 步骤4:保存文档

[翻译]
在添加所需内容后,使用
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");
  • save /sev/ 保存
  • content /'kɑntent/ 内容

Step 5: Closing the Document 步骤5:关闭文档

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

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

document.close();
  • finally /'fanli/ 最后
  • close /kloz/ 关闭

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.

  • suppose /s'poz/ 假设
  • path /paeθ/ 路径

[翻译]
本示例演示如何在上述PDF文档中嵌入JavaScript。在此,我们将加载名为
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();

   }
}
  • demonstrate /'demnstret/ 演示
  • embed /m'bed/ 嵌入

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

[原文]
Compile and execute the saved Java file from the command prompt using the following commands.

javac AddJavaScript.java 
java AddJavaScript
  • compile /km'pal/ 编译
  • execute /'ekskjut/ 执行
  • command /k'maend/ 命令
  • prompt /prɑmpt/ 提示符

[翻译]
执行后,上述程序会在给定的PDF文档中嵌入JavaScript,并显示以下消息。

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

Data added to the given PDF
  • upon /'pɑn/ 在……时
  • display /d'sple/ 显示

[翻译]
如果您尝试打开
new.pdf文档,它将显示一个警告消息,如下图所示。

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

  • alert /'lrt/ 警告
  • message /'mesd/ 消息