在 C# 中编辑 PDF 格式

本机和高性能 PDF 文档敏感密文信息使用服务器端 Aspose.PDF for C# API,无需使用微软或 Adobe PDF 等任何软件。

如何使用 C# 库编辑 PDF 文件

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

通过 C# 编辑 PDF 文档


你需要 Aspose.PDF for .NET 在你的环境中试用代码。

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