如何为 PDF 文件加密/添加密码和权限

查克尼特·宾
查克尼特·宾
2024年十月20日
更新 2024年十二月17日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

使用密码和特定权限保护 PDF 文件对于保护敏感文件免遭未经授权的访问和篡改至关重要。 IronSecure Doc 的 [POST] 加密/添加密码和权限 API 提供了一种高效的方法,可以使用用户和所有者密码加密 PDF 文件,同时配置打印、表单填写、注释等权限。 该 API 可确保 PDF 得到充分保护,并对访问进行控制,是法律、财务或个人文件的理想选择。

拉动并启动 IronSecureDoc

如果您还没有运行 IronSecureDoc,请按照以下链接进行设置:

Host LocallyDeploy to Cloud

POST] 加密/添加密码和权限 API

[POST] Encrypt/Add Password & Permissions API 允许您通过使用密码加密 PDF 文档并为各种操作设置特定权限来添加安全层。 无论是授予读取权限、允许填写表格还是禁用打印,该 API 都能让您控制如何访问和修改 PDF 文件。

Swagger

Swagger 提供了一个用户友好型界面,可对该 API 进行交互式测试。 您可以使用它发送测试请求并接收响应,而无需编写代码,因此是开发人员的理想工具。

使用 Swagger 重制文本的步骤

  1. 访问 Swagger UI:

    如果您的 API 服务器在本地运行,您可以通过在 Web 浏览器中导航到 http://localhost:8080/swagger/index.html 访问 Swagger。

    Swagger 文档

  2. 定位 API:

    查找 [POST] /v1/document-services/pdfs/encrypt API,对应于加密/添加密码和权限。

    编辑文本

  3. 输入参数:

    提供 API 所需的参数。 您可以上传 PDF 样本文件,并指定所需的权限。

  4. 上传 PDF 文件:

    在请求正文中,上传 PDF 文件作为 pdf_file,并定义必要的权限,如允许或限制打印、表格填写和内容提取。

  5. 执行请求:

    设置好所有参数后,点击 "执行 "发送请求。响应将返回指定安全设置的加密 PDF。

  6. 检查输出 PDF:

    输入 "密码 "查看 PDF 内容。


理解输入参数

API 需要特定的参数来加密 PDF 和分配权限。 以下是必填参数和可选参数的细目:

必需参数

NameData TypeDescription
pdf_fileapplication/pdfThe PDF file you want to encrypt.
allow_extracting_contentbooleanIndicate whether content extraction is allowed.
allow_form_fillingbooleanIndicate whether form filling is allowed.
allow_annotationsbooleanIndicate whether annotations are allowed.
allow_printingbooleanIndicate whether printing is allowed.
allow_modificationsbooleanIndicate whether modifications are allowed.
new_owner_passwordstringThe new owner password for the PDF, which grants full access and the ability to change permissions.

可选参数

NameData TypeDescription
user_passwordstringThe current user password, required if the PDF has a user password to grant read access.
owner_passwordstringThe current owner password, required if the PDF has an owner password to grant full access.
new_user_passwordThe new user password for read access.
save_as_pdfabooleanSave the PDF as PDF/A-3 compliant.
save_as_pdfuabooleanSave the PDF as PDF/UA compliant.
authorstringSet the PDF metadata Author property.
titlestringSet the PDF metadata Title property.
subjectstringSet the PDF metadata Subject property.

API 集成:Python 示例

一旦熟悉了输入参数,您就可以使用 Python 或任何其他首选语言调用 API。 下面是一个如何使用 Python 集成此 API 的示例。

import requests

url = 'http://localhost:8080/v1/document-services/pdfs/encrypt'
headers = {
    'accept': '*/*',
    'author': 'IronSoftware',
    'title': 'ENCRYPTION DEMO 2024',
    'subject': 'DEMO EXAMPLE'
}

files = {
    'pdf_file': ('sample.pdf', open('sample.pdf', 'rb'), 'application/pdf')
}

data = {
    'allow_extracting_content': 'true',
    'allow_form_filling': 'true',
    'allow_annotations': 'false',
    'allow_printing': 'false',
    'allow_modifications': 'true',
    'new_owner_password': 'password',
    'new_user_password': 'password',
    'save_as_pdfa': 'false',
    'save_as_pdfua': 'false'
}

response = requests.post(url, headers=headers, files=files, data=data)

# Save the encrypted PDF
with open('sample.pdf', 'wb') as f:
    f.write(response.content)

print('PDF encrypted successfully.')
PYTHON

步骤说明:

  • 加载PDF:要加密的PDF文件从本地文件系统加载。
  • 设置加密参数:定义权限,例如允许打印、填写表单和内容提取,并设置新密码。
  • 调用API:调用 [POST] 加密/添加密码和权限 API,传递必要的参数。
  • 保存结果:加密的PDF被保存为一个新文件。

    PDF 文件已加密,如下所示。 输入 "密码 "查看 PDF 内容。

查克尼特·宾
软件工程师
Chaknith 负责 IronXL 和 IronBarcode 的工作。他在 C# 和 .NET 方面拥有深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的洞察力,有助于提升产品、文档和整体体验。