如何在 PDF 檔案中遮蔽特定區域 with C

This article was translated from English: Does it need improvement?
Translated
View the article in English

在 PDF 文件中遮蔽敏感資訊,對於確保隱私及遵守資料保護法規至關重要。 IronSecure Doc 提供的 [POST] Redact Region API 提供了一種高效的方式,可透過真正的遮蔽技術,隱藏 PDF 文件中特定區域的敏感文字與資訊。 此 API 可確保經遮蔽處理的資料被徹底移除且無法復原,因此非常適合用於處理法律、財務或個人文件中的機密資訊。

拉取並啟動 IronSecureDoc

若您尚未執行 IronSecureDoc,請點擊下方連結進行設定:

本地部署部署至雲端

[POST] 區域遮蔽 API

[POST] Redact Region API 端點可讓您透過真正的遮蔽技術,隱藏 PDF 文件中特定區域內的敏感資訊。 此功能對於管理機密文件(例如法律合約、醫療紀錄或財務報表)的應用程式至關重要。 透過運用此 API,您可以確保 PDF 文件中指定區域內的敏感文字被永久移除,同時兼顧安全性與合規性。

請注意一旦區域被遮蔽,該區域內的內容將無法恢復。

在 Swagger 中試用

Swagger 是一款強大的工具,可讓開發人員透過友善的網頁介面與 RESTful API 進行互動。 無論您使用的是 Python、Java 或其他語言,Swagger 都能提供便捷的方式來測試並實作此 API。

使用 Swagger 遮蔽區域的步驟

  1. 存取 Swagger UI:

    若您的 API 伺服器在本地端運行,您可透過在網頁瀏覽器中導航至 http://localhost:8080/swagger/index.html 來存取 Swagger。

    Swagger 文件

  2. 定位 [POST] /v1/document-services/pdfs/redact-region 端點:

    在 Swagger UI 中,找到 POST /v1/document-services/pdfs/redact-region 端點。

    遮蔽區域

  3. 指定遮蔽座標:

    在此範例中,我們將從 PDF 的索引頁 1(即第 2 頁)移除一個表格。 請使用以下座標定義遮蔽區域:

    • 頁面索引 (specific_pages): 1
    • X 座標 (region_to_redact_x): 60
    • Y 座標 (region_to_redact_y): 270
    • 字寬 (region_to_redact_w): 470
    • 高度 (region_to_redact_h): 200
  4. 設定可選參數:

    您可選擇性地設定使用者或擁有者密碼、指定特定頁面,或決定是否在遮蔽區域繪製黑色方框,並以符合 PDF/A 或 PDF/UA 標準的格式儲存文件。

    Swagger 輸入

  5. 上傳範例 PDF:

    請在請求內容中上傳一個您希望進行遮蔽處理的樣本 PDF 檔案。 請確保檔案以 pdf_file 的格式加入。

  6. 執行請求:

    點擊"執行"以執行請求。回應將包含經處理的 PDF 檔案,其中第 1 頁的表格已依照指定移除。

    回應

    此 Swagger UI 互動功能讓您能輕鬆測試遮蔽處理流程,並即時提供座標如何影響 PDF 內容的回饋。

  7. 檢查輸出 PDF:

    被遮蔽的區域將位於第 2 頁。


理解輸入參數

在使用此 API 之前,務必了解在 PDF 中遮蔽特定區域時所需的必填與選填輸入參數。 這些參數有助於界定需進行刪節的具體範圍。

關鍵參數

  • pdf_file您要進行遮蔽處理的 PDF 文件。
  • region_to_redact_x需遮蔽區域的 X 座標(以頁面左下角為起點)。
  • region_to_redact_y需遮蔽區域的 Y 座標(以頁面左下角為起點)。
  • region_to_redact_w需遮蔽區域的寬度。
  • region_to_redact_h需遮蔽區域的高度。

可選參數

  • user_password若 PDF 檔案設有密碼保護,請提供使用者密碼。
  • owner_password若修改權限受限,請提供擁有者密碼。
  • specific_pages指定需遮蔽的內容頁面。 若未另行指定,此修訂內容適用於所有頁面。
  • save_as_pdfa以符合 PDF/A-3 標準的方式儲存 PDF 檔案。
  • save_as_pdfua以符合 PDF/UA 標準的方式儲存 PDF 檔案。

API 整合:Python 範例

熟悉相關參數後,即可使用您偏好的程式語言呼叫此 API。 以下是使用 Python 整合此 API 的範例。

import requests

# Define the API endpoint URL
url = 'http://localhost:8080/v1/document-services/pdfs/redact-region'

# Set the headers for the request (optional relevant metadata)
headers = {
    'accept': '*/*',
    'author': 'Iron Software',
    'title': 'REDACT REGION DEMO 2024',
    'subject': 'DEMO EXAMPLE'
}

# Open the PDF file to be redacted in binary read mode
files = {
    'pdf_file': ('sample_file.pdf', open('sample_file.pdf', 'rb'), 'application/pdf')
}

# Define the coordinates and page for the redaction region
data = {
    'region_to_redact_x': '60',  # X-coordinate starting at the bottom-left
    'region_to_redact_y': '270', # Y-coordinate starting at the bottom-left
    'region_to_redact_w': '470', # Width of the region to be redacted
    'region_to_redact_h': '200', # Height of the region to be redacted
    'specific_pages': [1]        # Specify the page index to redact
}

# Make the POST request to the API with the provided parameters and file
response = requests.post(url, headers=headers, files=files, data=data)

# Save the redacted PDF response to a new file
with open('redacted_output.pdf', 'wb') as f:
    f.write(response.co/ntent)

print('PDF redacted successfully.')
import requests

# Define the API endpoint URL
url = 'http://localhost:8080/v1/document-services/pdfs/redact-region'

# Set the headers for the request (optional relevant metadata)
headers = {
    'accept': '*/*',
    'author': 'Iron Software',
    'title': 'REDACT REGION DEMO 2024',
    'subject': 'DEMO EXAMPLE'
}

# Open the PDF file to be redacted in binary read mode
files = {
    'pdf_file': ('sample_file.pdf', open('sample_file.pdf', 'rb'), 'application/pdf')
}

# Define the coordinates and page for the redaction region
data = {
    'region_to_redact_x': '60',  # X-coordinate starting at the bottom-left
    'region_to_redact_y': '270', # Y-coordinate starting at the bottom-left
    'region_to_redact_w': '470', # Width of the region to be redacted
    'region_to_redact_h': '200', # Height of the region to be redacted
    'specific_pages': [1]        # Specify the page index to redact
}

# Make the POST request to the API with the provided parameters and file
response = requests.post(url, headers=headers, files=files, data=data)

# Save the redacted PDF response to a new file
with open('redacted_output.pdf', 'wb') as f:
    f.write(response.co/ntent)

print('PDF redacted successfully.')
PYTHON

此程式碼執行以下步驟:

  • 載入 PDF:將從本機檔案系統載入待遮蔽的 PDF 檔案。
  • 設定遮蔽參數:指定要遮蔽的座標 (X, Y)、寬度、高度及特定頁面。
  • 呼叫 API:呼叫 [POST] Redact Region API,並傳入必要的參數。
  • 儲存結果:經編輯的 PDF 將儲存為新檔案。

指定區域已如以下所示進行遮蔽。

已刪除的輸出內容

常見問題

如何在 PDF 檔案中遮蔽特定區域?

您可以使用 IronSecureDoc 的 [POST] Redact Region API 來遮蔽 PDF 檔案中的特定區域。透過提供遮蔽區域的座標與尺寸,此 API 可確保敏感資訊從文件中永久移除。

設定 IronSecureDoc API 進行資料遮蔽需經過哪些步驟?

若要設定 IronSecureDoc API 進行遮蔽處理,您需要拉取並啟動 Docker 映像檔、透過 Swagger 配置 API、指定遮蔽參數,並執行 API 呼叫以對 PDF 文件中的區域進行遮蔽處理。

IronSecureDoc 能否在雲端平台上使用?

是的,IronSecureDoc 可部署於 Azure 和 AWS 等雲端平台,提供可擴展且靈活的資料遮蔽解決方案。

如何使用 IronSecureDoc 指定 PDF 中的哪些區域需要遮蔽?

若要使用 IronSecureDoc 指定遮蔽區域,您需提供 X 和 Y 座標,以及待遮蔽區域的寬度和高度。這些參數將定義 PDF 頁面上的確切區域。

是否有方法能在全面實施前測試這套修訂流程?

是的,您可以透過在本地端執行 IronSecureDoc API 伺服器,並使用 Swagger 與 API 互動,來測試遮蔽處理流程。這讓您能在全面部署前,測試遮蔽參數並驗證輸出結果。

可以使用哪些程式語言與 IronSecureDoc API 進行整合?

IronSecureDoc API 可與任何能發送 HTTP 請求的程式語言整合,例如 Python、Java、C# 等。

PDF 中的真正遮蔽功能是什麼?為何它如此重要?

PDF 檔案中的真正遮蔽功能,可確保敏感資料不僅被隱藏,更會從文件中徹底移除。這對於維護機密性及遵守資料保護法規至關重要。

IronSecureDoc 是否支援 PDF 合規標準?

是的,使用 IronSecureDoc 儲存經遮蔽處理的 PDF 時,您可以選擇遵循 PDF/A-3 或 PDF/UA 等標準,以滿足特定的文件要求。

IronSecureDoc 能否處理受密碼保護的 PDF 檔案以進行遮蔽處理?

是的,IronSecureDoc 能夠處理受密碼保護的 PDF 檔案,在遮蔽處理過程中,可將所需的使用者密碼和擁有者密碼作為選填參數提供。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
版本: 2024.10 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎?
執行範例 觀看您的 PDF 被加密。