如何加密/添加密碼與權限到 PDF 文件

查克尼思·賓
查克尼思·賓
2024年10月20日
已更新 2024年12月17日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

使用密碼和特定權限來保護 PDF 文件,對於防止敏感文件遭未授權存取和篡改至關重要。 IronSecure Doc 的 [POST] Encrypt/Add Password & Permissions API 提供了一種高效的方法,可以使用用戶和擁有者密碼加密 PDF 文件,同時配置列印、表單填寫、註釋等權限。 此 API 確保 PDF 完全受到保護並且可以控制存取,非常適合用於法律、財務或個人文件。

提取並啟動IronSecureDoc

如果您還沒有運行 IronSecureDoc,請按照以下鏈接進行設置:

本地託管

[POST] 加密/添加密碼與權限 API

[POST] 加密/新增密碼與權限 API 允許您通過使用密碼加密來為 PDF 文件添加安全層,並為各種操作設置特定的權限。 無論是授予讀取權限、允許填寫表單,還是禁用列印,此 API 都讓您能夠控制 PDF 檔案的訪問和修改方式。

Swagger

Swagger 提供使用者友好的介面來互動測試此 API。 您可以使用它发送测试请求并接收响应,而无需编写代码,使其成为开发者的绝佳工具。

使用 Swagger 編輯文本的步驟

  1. 訪問 Swagger UI:

    如果你的 API 伺服器在本地運行,可以在網頁瀏覽器中導航至 http://localhost:8080/swagger/index.html 來訪問 Swagger。

    Swagger 文件

  2. 定位 API:

    查找[POST] /v1/document-services/pdfs/encrypt API,對應於加密/添加密碼和權限。

    編輯文本

  3. 輸入參數:

    提供 API 所需的參數。 您可以上傳範例 PDF 文件並指定所需的權限。

  4. 上傳 PDF 文件:

    在請求正文中,上傳作為 pdf_file 的 PDF 文件,並定義必要的權限,比如允許或限制列印、填寫表單和內容提取。

  5. 執行請求:

    設定所有參數後,點擊「執行」以發送請求。回應將返回設有指定安全設定的加密PDF。

  6. 檢查輸出 PDF:

    輸入「password」以查看 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 檔案已加密,如下所示。 輸入「password」以查看 PDF 內容。

查克尼思·賓
軟體工程師
Chaknith 致力於 IronXL 和 IronBarcode。他在 C# 和 .NET 方面擁有豐富的專業知識,協助改進軟體並支持客戶。他從用戶互動中獲得的洞察力有助於提高產品、文檔和整體體驗。