PDF 編輯

使用免費的跨平台應用程序和 API 查找和刪除 PDF 文檔中的內容

如何編輯 PDF 文件

為了編輯 PDF 文件,我們將使用 Aspose.PDF API,它是一款功能豐富、強大且易於使用的文件操作 API,適用於任何平台。開啟 NuGet 套件管理器,搜尋 Aspose.PDF 並安裝。您也可以在套件管理器控制台中使用下列命令。

編輯 PDF 文件


您需要 Aspose.PDF 庫 才能在您的環境中嘗試該程式碼。

1.載入帶有「文件」實例的 PDF。 1.使用搜索詞作為引數創建文本 FragmentAbsorber 對象。 1.設定搜尋選項。 1.循環穿過每個收集片段以編輯。 1.儲存 PDF 檔案。

編輯 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");