PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
PDF 权限模块用于确保 PDF 文档的安全性,提供加密、文档权限、解密、移除密码功能,以保障用户对文档的安全控制和有效管理。
加密
加密功能分为用户密码和所有者密码两部分。用户密码用于打开文档,确保只有授权的用户可以访问文档内容。当设置用户密码时,通常会限制一些诸如修改、复制或打印等文档权限。而所有者密码不仅可以打开文档,还能解锁所有被禁止的权限,使用户能够对文档进行修改、复制或打印等操作。双层密码体系旨在提供更加灵活和安全的文档访问和管理方式。
ComPDFKit 提供多种加密算法和权限设置,根据需求使用合适的算法,设置自定义权限以保护文档。
以下是加密的步骤:
以下是加密 PDF 文档的示例代码:
// Open document from file path.
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
// Set user password.
document.setUserPassword(user_password);
// Set owner password.
document.setOwnerPassword(owner_password);
// Set permission info.
CPDFDocumentPermissionInfo info = document.getPermissionsInfo();
info.setAllowsPrinting(false);
info.setAllowsCopying(true);
document.setPermissionsInfo(info);
// Save document.
document.save(CPDFDocument.PDFDocumentSaveType.PDFDocumentSaveNoIncremental);文档权限
PDF 规范中,支持对文档的多种权限进行设置,通过设置这些权限,可以限制用户只能做出符合预期的行为。
PDF 规范定义了如下所示的权限:
以下是查看文档权限的步骤:
以下是查看文档权限的示例代码:
// Open document from file path.
CPDFDocument document = new CPDFDocument();
document.open(pdfPath);
// get permission info.
CPDFDocumentPermissionInfo info = document.getPermissionsInfo();
System.out.println("Allows Printing:" + info.isAllowsPrinting());
System.out.println("Allows Copying:" + info.isAllowsCopying());解密
访问具有密码保护的 PDF 文档,需要输入密码。ComPDFKit 提供了两种密码:用户密码和所有者密码。用户密码用于打开文档,确保只有授权的用户可以访问文档内容。而所有者密码不仅打开文档,还解锁所有权限,使用户能够对文档进行修改、打印或复制等操作。双层密码体系旨在提供更加灵活和安全的文档访问和管理方式。
使用不同级别的密码获取不同的权限级别。
解密的步骤如下:
以下是解密 PDF 文档的示例代码:
// Open document from file path.
CPDFDocument document = new CPDFDocument();
CPDFDocument.PDFDocumentError error = document.open(pdfPath);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
// Password required.
document.open(pdfFath, password);
}删除密码
删除密码是指用户在已经获得了所有者权限的情况下,将文档的所有者密码和用户密码移除并另存为新的文档,新的文档将不再需要密码即可打开且默认可使用所有权限。
删除密码的步骤如下:
以下是删除密码的示例代码:
// Open document from file path.
CPDFDocument document = new CPDFDocument();
document.open(pdfPath, password);
// Remove password.
try {
document.save(CPDFDocument.PDFDocumentSaveType.PDFDocumentSaveRemoveSecurity);
} catch (CPDFDocumentException e) {
e.printStackTrace();
}