如何加密/添加密碼與權限到 PDF 文件
使用密碼和特定權限來保護 PDF 文件,對於防止敏感文件遭未授權存取和篡改至關重要。 IronSecure Doc 的 [POST] Encrypt/Add Password & Permissions API 提供了一種高效的方法,可以使用用戶和擁有者密碼加密 PDF 文件,同時配置列印、表單填寫、註釋等權限。 此 API 確保 PDF 完全受到保護並且可以控制存取,非常適合用於法律、財務或個人文件。
How to Encrypt/Add Password & Permissions to PDF Files
- 拉取並啟動 IronSecureDoc Docker 映像
- 使用 Swagger 測試 API
- 指定密碼和權限的參數
- 執行 API 呼叫以套用變更
- 匯出加密的 PDF 文件
提取並啟動IronSecureDoc
如果您還沒有運行 IronSecureDoc,請按照以下鏈接進行設置:
[POST] 加密/添加密碼與權限 API
[POST] 加密/新增密碼與權限 API 允許您通過使用密碼加密來為 PDF 文件添加安全層,並為各種操作設置特定的權限。 無論是授予讀取權限、允許填寫表單,還是禁用列印,此 API 都讓您能夠控制 PDF 檔案的訪問和修改方式。
Swagger
Swagger 提供使用者友好的介面來互動測試此 API。 您可以使用它发送测试请求并接收响应,而无需编写代码,使其成为开发者的绝佳工具。
使用 Swagger 編輯文本的步驟
訪問 Swagger UI:
如果你的 API 伺服器在本地運行,可以在網頁瀏覽器中導航至 http://localhost:8080/swagger/index.html 來訪問 Swagger。
定位 API:
查找[POST] /v1/document-services/pdfs/encrypt API,對應於加密/添加密碼和權限。
輸入參數:
提供 API 所需的參數。 您可以上傳範例 PDF 文件並指定所需的權限。
上傳 PDF 文件:
在請求正文中,上傳作為 pdf_file 的 PDF 文件,並定義必要的權限,比如允許或限制列印、填寫表單和內容提取。
執行請求:
設定所有參數後,點擊「執行」以發送請求。回應將返回設有指定安全設定的加密PDF。
檢查輸出 PDF:
輸入「password」以查看 PDF 內容。
了解輸入參數
該 API 需要特定參數來加密 PDF 並指派權限。 以下是必須參數和可選參數的詳細說明:
必填參數
Name | Data Type | Description |
---|---|---|
pdf_file | application/pdf | The PDF file you want to encrypt. |
allow_extracting_content | boolean | Indicate whether content extraction is allowed. |
allow_form_filling | boolean | Indicate whether form filling is allowed. |
allow_annotations | boolean | Indicate whether annotations are allowed. |
allow_printing | boolean | Indicate whether printing is allowed. |
allow_modifications | boolean | Indicate whether modifications are allowed. |
new_owner_password | string | The new owner password for the PDF, which grants full access and the ability to change permissions. |
選擇性參數
Name | Data Type | Description |
---|---|---|
user_password | string | The current user password, required if the PDF has a user password to grant read access. |
owner_password | string | The current owner password, required if the PDF has an owner password to grant full access. |
new_user_password | The new user password for read access. | |
save_as_pdfa | boolean | Save the PDF as PDF/A-3 compliant. |
save_as_pdfua | boolean | Save the PDF as PDF/UA compliant. |
author | string | Set the PDF metadata Author property. |
title | string | Set the PDF metadata Title property. |
subject | string | Set 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.')
步驟說明:
- 載入 PDF:要加密的 PDF 檔案從本地檔案系統載入。
- 設置加密參數:定義許可權,如允許列印、表單填寫和內容提取,以及設置新密碼。
- 呼叫 API:呼叫 [POST] 加密/新增密碼和權限 API,並傳遞必要的參數。
保存結果:加密的 PDF 被保存為新文件。
PDF 檔案已加密,如下所示。 輸入「password」以查看 PDF 內容。