在C#中編輯 PDF 格式

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

如何使用 C# 庫編輯PDF檔

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

通過C#編輯 PDF 文件


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

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

編輯 PDF 檔案 - C#


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

    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchTerm);
    TextSearchOptions textSearchOptions = new TextSearchOptions(true);
    textFragmentAbsorber.TextSearchOptions = textSearchOptions;

    doc.Pages.Accept(textFragmentAbsorber);
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
    foreach (TextFragment textFragment in textFragmentCollection)
{
    Page page = textFragment.Page;
    Rectangle annotationRectangle = textFragment.Rectangle;
    
    Annotations.RedactionAnnotation annot = new Annotations.RedactionAnnotation(page, annotationRectangle);
    annot.FillColor = Color.Black;
    doc.Pages[textFragment.Page.Number].Annotations.Add(annot, true);
    annot.Redact();
}
    doc.Save(dataDir + "output.pdf");