网站首页 > java教程 正文
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文档
[翻译]
您可以使用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/ 输入
猜你喜欢
- 2025-07-06 10.JAVA向PDF中添加多行文本(PDFBOX)
- 2025-07-06 提取PDF中的表格数据——tabula-py库
- 2025-07-06 一次Java内存占用高的排查案例,解释了我对内存问题的所有疑问
- 2025-07-06 已跪,Java全能笔记爆火,分布式/开源框架/微服务/性能调优全有
- 2025-07-06 一线大厂Java八股文合集PDF版分享,内容多达700多页
- 2025-07-06 【Java面试必问】 Spring Boot面试题 35 问(附PDF)
- 2025-07-06 阿里Nacos惊爆安全漏洞,火速升级!(附修复建议)
- 2025-07-06 一文看懂 ZooKeeper,面试再也不用背八股(文末送PDF)
- 2025-07-06 17.从PDF文档生成图像(JAVA+PDFBOX)
- 2025-07-06 15.将一个给定的PDF文档拆分为多个文档(JAVA+PDFBOX)
你 发表评论:
欢迎- 最近发表
-
- 五,网络安全IDA Pro反汇编工具初识及逆向工程解密实战
- 「JAVA8」- Lambda 表达式(java lambda表达式原理)
- 深入探讨Java代码保护:虚拟机保护技术的新时代
- Nginx反向代理原理详解(图文全面总结)
- 逆向拆解日本IT,哪些Java技术栈薪资溢价高
- mybatis 逆向工程使用姿势不对,把表清空了,心里慌的一比
- Spring Boot集成ProGuard轻松实现Java 代码混淆, Java 应用固若金汤
- 从 Java 代码逆向工程生成 UML 类图和序列图
- 人与人相处:尊重是标配,靠谱是高配,厚道是顶配
- Windows系统安装日期如何修改(windows10怎么修改安装日期)
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)