如何使用IronSecureDoc在PDF上編輯文字

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

在本文中,我們將討論如何使用IronSecureDoc在PDF上編輯文字。 這允許服務或流程通過向運行中的IronSecureDoc伺服器發送帶有PDF的簡單POST請求,快速輕鬆地編輯敏感資訊。 我們將通過使用 Swagger 文件以視覺化的方式展示這一點。 POST 請求接收必要和可選參數,並且高度可定制; 回應返回包含已編輯文字的 PDF。

提取並啟動IronSecureDoc

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

本地託管

[POST] 紅色文字 API

[POST] Redact Text API 端點允許您使用編輯功能隱藏 PDF 文件中的敏感文本。 此功能對於處理機密文件的應用程式至關重要,例如法律合同、醫療記錄或財務報告。 透過使用此 API,您可以確保特定文本被永久移除,提升安全性並確保符合資料保護標準。

請注意
一旦文本被編輯,內容將無法恢復。

Swagger

Swagger 是一個強大的工具,讓開發人員能夠通過用戶友好的網頁介面與 RESTful API 進行互動。 無論您使用的是 Python、Java 或其他語言,Swagger 都提供了一種便捷的方法來測試和實施這個 API。

使用 Swagger 編輯文本的步驟

  1. 訪問 Swagger UI:

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

    Swagger 文件

  2. 定位 [POST] Redact Text API:

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

    隱藏文字

  3. 指定配置:

    在此範例中,我在 POST 請求中同時提供了 PDF 文件和需要涂黑的文字。我們將涂黑單詞「we」並在其上覆蓋一個黑色方塊。 在此示範中,我們將使用具有以下配置的 'sample.pdf' 文件:

    • draw_black_box: true
    • 匹配整個單詞:真
    • words_to_redact: 我們
  4. 上傳範例PDF:

    在請求主體中,上傳您要進行編輯的範例 PDF 文件。 確保該檔案已新增為pdf_file

  5. 執行請求:

    點擊「執行」以運行請求。回應將包含被編輯的 PDF。 此 Swagger UI 互動介面讓您可以輕鬆測試編輯過程,並提供即時回饋。

    了解結果

在 Swagger 文档中输入所需参数后,我们执行 POST 请求。 如果成功,請求將返回一個200狀態碼,並附上可下載的修改後PDF文件,如上例所示。

上面的修改後的 PDF 顯示了出現「we」一詞的區域,並以黑框替換。 此外,match_whole_word 布林值被設置為 True,這表示我們只遮蔽整個單詞,而不會在單詞中部分匹配。

-->


透過命令提示字元使用 CURL 請求

或者,我們可以使用命令提示字元與 curl POST 請求來達到相同的結果。

curl -X POST 'http://localhost:8080/v1/document-services/pdfs/redact-text' \
 -H 'accept: */*' \
 -H 'Content-Type: multipart/form-data' \
 -F 'pdf_file=@sample.pdf;type=application/pdf' \
 -F 'words_to_redact="we"' \
 -F 'draw_black_box=true' \
 -F 'match_whole_word=true'

(預設情況下,PowerShell 可能將 curl 解釋為 Invoke-WebRequest 的別名,這是一個內建的 PowerShell cmdlet。 試試用 curl.exe 代替 curl。

curl.exe --version

)}]

所需請求正文參數

NameData TypeDescription
pdf_fileapplication/pdfThe PDF file you want to manipulate.
words_to_redactarray[string]This parameter takes a list of words and redacts the text matching the input.

可選請求正文參數

NameData TypeDescription
user_passwordstringThis is required if the input PDF has a user password. The operation will fail if no password is provided for the password-protected PDF.
owner_passwordstringThis is required if the input PDF has an owner password. The operation will fail if no password is provided for the password-protected PDF.
specific_pagesarray[int]Allows you to specify which pages to redact text on. By default, the value is null, meaning the provided word in all the pages will be redacted.
draw_black_boxbooleanAllows you to specify whether to draw a black box over the redacted text. By default, this value is set to True.
match_whole_wordbooleanSpecifies whether partial matches within words should also be redacted. For example, if the provided word is "are," any words containing "are," such as "hare," will have the "are" redacted as well. By default, this is set to True.
match_casebooleanSpecifies whether the provided word should be an exact match in terms of case. By default, this value is null. Note: Setting this to True means that lowercase and uppercase strings will not be matched. For example, if the provided word is "WE," the lowercase version "we" would not be redacted.
overlay_textstringIt specifies the overlay text, such as words or symbols, over the redacted text. By default, this string is empty.
save_as_pdfabooleanSaves the modified PDF with PDF/A-3 compliance. By default, this is set to False.
save_as_pdfuabooleanSaves the modified PDF with PDF/UA compliance. By default, this is set to False.

選用標頭參數

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