专业的JAVA编程教程与资源

网站首页 > java教程 正文

13.加密PDF文档(JAVA+PDFBOX)(加密文件java)

temp10 2025-07-06 16:22:47 java教程 2 ℃ 0 评论

In the previous chapter, we have seen how to insert an image in a PDF document. In this chapter, we will discuss how to encrypt a PDF document.
[上一章中,我们已经了解了如何在PDF文档中插入图片。在本章中,我们将讨论如何加密PDF文档。]


Encrypting a PDF Document
加密PDF文档

13.加密PDF文档(JAVA+PDFBOX)(加密文件java)

[翻译]
您可以使用StandardProtectionPolicy类和AccessPermission类提供的方法来加密PDF文档。

[原文]
You can encrypt a PDF document using the methods provided by StandardProtectionPolicy and AccessPermission classes.

StandardProtectionPolicy /staendrd pr'tekn 'pɑlsi/ 标准保护策略
AccessPermission /'aekses pr'mn/ 访问权限
encrypt /n'krpt/ 加密
provided /pr'vadd/ 提供的


[翻译]
AccessPermission类用于通过分配访问权限来保护PDF文档。使用该类,您可以限制用户执行以下操作:

[原文]
The AccessPermission class is used to protect the PDF Document by assigning access permissions to it. Using this class, you can restrict users from performing the following operations.

assigning /'san/ 分配
restrict /r'strkt/ 限制
performing /pr'frm/ 执行
operations /ɑp'renz/ 操作


[翻译]

  • 打印文档
  • 修改文档内容
  • 复制或提取文档内容
  • 添加或修改注释
  • 填充交互式表单字段
  • 为视障人士提取文本和图形以实现可访问性
  • 组装文档
  • 以降低的质量打印

[原文]

  • Print the document
  • Modify the content of the document
  • Copy or extract content of the document
  • Add or modify annotations
  • Fill in interactive form fields
  • Extract text and graphics for accessibility to visually impaired people
  • Assemble the document
  • Print in degraded quality

annotations /aen'tenz/ 注释
interactive /ntr'aektv/ 交互式的
accessibility /kses'blti/ 可访问性
visually impaired /'vuli m'perd/ 视障
assemble /'sembl/ 组装
degraded /d'ɡredd/ 降低的


[翻译]
StandardProtectionPolicy类用于为文档添加基于密码的保护。

[原文]
The StandardProtectionPolicy class is used to add a password based protection to a document.

password /'paeswrd/ 密码
protection /pr'tekn/ 保护


[翻译]
以下是加密现有PDF文档的步骤:

[原文]
Following are the steps to encrypt an existing PDF document.

existing /ɡ'zst/ 现有的
steps /steps/ 步骤


[翻译]
步骤1:加载现有PDF文档
使用PDDocument类的静态方法load()加载现有PDF文档。该方法接受一个文件对象作为参数,由于这是一个静态方法,您可以使用类名调用它,如下所示。

[原文]
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);

static /'staetk/ 静态的
invoke /n'vok/ 调用
parameter /p'raemtr/ 参数


[翻译]
步骤2:创建访问权限对象
如下所示实例化AccessPermission类。

[原文]
Step 2: Creating Access Permission Object
Instantiate the AccessPermission class as shown below.

AccessPermission accessPermission = new AccessPermission();

instantiate /n'staeniet/ 实例化
object /'ɑbdekt/ 对象


[翻译]
步骤3:创建标准保护策略对象
通过传递所有者密码、用户密码和AccessPermission对象来实例化StandardProtectionPolicy类,如下所示。

[原文]
Step 3: Creating StandardProtectionPolicy Object
Instantiate the StandardProtectionPolicy class by passing the owner password, user password, and the AccessPermission object as shown below.

StandardProtectionPolicy spp = new StandardProtectionPolicy("1234","1234",accessPermission);

owner /'onr/ 所有者
user /'juzr/ 用户


[翻译]
步骤4:设置加密密钥长度
使用setEncryptionKeyLength()方法设置加密密钥长度,如下所示。

[原文]
Step 4: Setting the Length of the Encryption Key
Set the encryption key length using the setEncryptionKeyLength() method as shown below.

spp.setEncryptionKeyLength(128);

encryption key /n'krpn ki/ 加密密钥
length /leθ/ 长度


[翻译]
步骤5:设置权限
使用StandardProtectionPolicy类的setPermissions()方法设置权限。该方法接受一个AccessPermission对象作为参数。

[原文]
Step 5: Setting the Permissions
Set the permissions using the setPermissions() method of the StandardProtectionPolicy class. This method accepts an AccessPermission object as a parameter.

spp.setPermissions(accessPermission);

permissions /pr'mnz/ 权限


[翻译]
步骤6:保护文档
使用PDDocument类的protect()方法保护文档,如下所示。将StandardProtectionPolicy对象作为参数传递给该方法。

[原文]
Step 6: Protecting the Document
You can protect your document using the protect() method of the PDDocument class as shown below. Pass the StandardProtectionPolicy object as a parameter to this method.

document.protect(spp);

protect /pr'tekt/ 保护


[翻译]
步骤7:保存文档
在添加所需内容后,使用PDDocument类的save()方法保存PDF文档,如以下代码块所示。

[原文]
Step 7: 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");

save /sev/ 保存
required /r'kward/ 所需的


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

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

document.close();

close /kloz/ 关闭


[翻译]
示例
假设我们有一个名为sample.pdf的PDF文档,位于C:/PdfBox_Examples/路径中,包含空白页面,如下所示。

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

Sample Document

suppose /s'poz/ 假设
empty /'empti/ 空的


[翻译]
此示例演示如何加密上述PDF文档。在此,我们将加载名为sample.pdf的PDF文档并对其进行加密。将此代码保存为名为EncriptingPDF.java的文件。

[原文]
This example demonstrates how to encrypt the above mentioned PDF document. Here, we will load the PDF document named sample.pdf and encrypt it. Save this code in a file with name EncriptingPDF.java.

import java.io.File;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
public class EncriptingPDF {

public static void main(String args[]) throws Exception {
//Loading an existing document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument document = PDDocument.load(file);

      //Creating access permission object
      AccessPermission ap = new AccessPermission();         

      //Creating StandardProtectionPolicy object
      StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);

      //Setting the length of the encryption key
      spp.setEncryptionKeyLength(128);

      //Setting the access permissions
      spp.setPermissions(ap);

      //Protecting the document
      document.protect(spp);

      System.out.println("Document encrypted");

      //Saving the document
      document.save("C:/PdfBox_Examples/sample.pdf");
      //Closing the document
      document.close();

}
}

demonstrates /'demnstrets/ 演示
mentioned /'mennd/ 提到的


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

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

javac EncriptingPDF.java java EncriptingPDF

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


[翻译]
执行后,上述程序将加密给定的PDF文档,并显示以下消息:

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

Document encrypted

upon /'pɑn/ 在……时
displaying /d'sple/ 显示


[翻译]
如果您尝试打开sample.pdf文档,您将无法打开,因为它已被加密。相反,它会提示您输入密码以打开文档,如下所示。

[原文]
If you try to open the document sample.pdf, you cannot, since it is encrypted. Instead, it prompts to type the password to open the document as shown below.

Document encryption

prompts /prɑmpts/ 提示
type /tap/ 输入


Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表