PDF 文档编辑解决方案

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

如何编辑 PDF 文件

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

编辑 PDF 文档


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

1。使用文档实例加载 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");