PDF 编辑

使用免费的跨平台应用程序和 API 在 PDF 文档中查找和删除内容

如何编辑 PDF 文件

为了编辑 PDF 文件,我们将使用 Aspose.PDF API,这是一个功能丰富、强大且易于使用的文档操作 API,适用于任何平台。打开 NuGet 软件包管理器,搜索 Aspose.PDF 并安装。您也可以使用包管理器控制台中的以下命令。

编辑 PDF 文档


你需要 Aspose.PDF 库 才能在你的环境中试用代码。

1。使用 Document 实例加载 PDF。 1。使用搜索词作为参数创建 TextFragmentAbsorber 对象。 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");