在Python中編輯 PDF 格式

本機和高性能 PDF 文檔敏感的編校資訊使用伺服器端 Aspose.PDF用於 Python API,而無需使用任何軟體,如微軟或 Adobe PDF。

如何使用 Python 庫編輯PDF檔

為了編輯PDF檔,我們將使用[Aspose.PDF用於.NET](https://products.aspose.com/pdf/net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 python-net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

通過Python編輯 PDF 文件


您需要 Aspose.PDF for .NET 在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。 創建文本碎片以搜索詞作為參數的吸收物件。
  2. 設定搜尋選項。
  3. 迴圈遍歷每個片段收集以進行密文。
  4. 儲存檔。

編輯 PDF 檔案 - Python


    doc = new Document(dataDir + "test.pdf")

    textFragmentAbsorber = TextFragmentAbsorber(searchTerm)
    textSearchOptions = TextSearchOptions(True)
    textFragmentAbsorber.TextSearchOptions = textSearchOptions

    doc.Pages.Accept(textFragmentAbsorber)
    textFragmentCollection = textFragmentAbsorber.TextFragments
    for textFragment in textFragmentCollection:
        page = textFragment.Page
        annotationRectangle = textFragment.Rectangle
        RedactionAnnotation annot = Annotations.RedactionAnnotation(page, annotationRectangle)
        annot.FillColor = Color.Black
        doc.Pages[textFragment.Page.Number].Annotations.Add(annot, true)
        annot.Redact()

        doc.Save(dataDir + "output.pdf")