本教程旨在指导您如何使用 C# 突出显示 PDF 中的文本,并使用简单的代码示例使过程清晰易懂。此外,它将提供有关如何利用注释库使用 C# 在 PDF 中突出显示文本的说明。 GroupDocs Annotation 是一个功能强大的库,可让您实时对 PDF 文档进行注释和协作。 GroupDocs Annotation 的主要功能之一是能够突出显示 PDF 文档中的文本。以下是使用此库在 PDF 中突出显示文本的步骤。
使用 C# 在 PDF 中突出显示文本的步骤
- 从 NuGet 安装 GroupDocs.Annotation for .NET 包
- 要突出显示 PDF 中的文本,您必须包含对 GroupDocs.Annotation 命名空间的引用
- 使用输入文档路径实例化 Annotator 对象
- 实例化 HighlightAnnotation 对象并设置它的一些属性
- 调用 Annotator.Add 方法并将 HighlightAnnotation 对象传递给它
- 使用生成的文档路径调用 Annotator.Save 方法
通过执行这些步骤,您将能够有效地突出显示 PDF 文档中的文本。这可能是一种很有价值的方式来强调关键信息、进行注释或评论,或者只是改进阅读的组织性。您可以使用上述步骤在任何支持 .NET 的系统上使用 C#* 突出显示 PDF 文本,而无需安装额外的软件。以下代码示例演示了如何在 PDF 文档中突出显示文本。
使用 C# 在 PDF 中突出显示文本的代码
using GroupDocs.Annotation.Models.AnnotationModels; | |
using GroupDocs.Annotation.Models; | |
using GroupDocs.Annotation; | |
using System; | |
using System.Collections.Generic; | |
namespace HighlightTextinPDFusingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Set License to avoid the limitations of Annotation library | |
License lic = new License(); | |
lic.SetLicense(@"GroupDocs.Annotation.lic"); | |
// Instantiate Annotator object with input PDF path | |
using (Annotator annotator = new Annotator("input.pdf")) | |
{ | |
// Initialize new instance of HighlightAnnotation class | |
HighlightAnnotation highlight = new HighlightAnnotation | |
{ | |
BackgroundColor = 65535, | |
CreatedOn = DateTime.Now, | |
FontColor = 0, | |
Message = "This is highlight annotation", | |
Opacity = 0.5, | |
PageNumber = 0, | |
Points = new List<Point> | |
{ | |
new Point(80, 730), new Point(240, 730), new Point(80, 650), new Point(240, 650) | |
}, | |
Replies = new List<Reply> | |
{ | |
new Reply | |
{ | |
Comment = "First comment", | |
RepliedOn = DateTime.Now | |
}, | |
new Reply | |
{ | |
Comment = "Second comment", | |
RepliedOn = DateTime.Now | |
} | |
} | |
}; | |
// Add highlight annotation | |
annotator.Add(highlight); | |
// Save final PDF to disk | |
annotator.Save("result.pdf"); | |
} | |
} | |
} | |
} |
在上一节中,我们提供了使用 C#* 在 PDF 中*突出显示文本的过程的综合指南,以及一个简单的代码示例。代码很简短,只涉及几个 API 调用来执行文本突出显示。一旦您安装了推荐的注释库并配置了文件路径,就可以毫不费力地将这些代码合并到您的项目中。
我们之前发表了一篇关于在 PDF 中添加下拉列表的文章;如需更多信息,请参阅如何使用C#在PDF中添加下拉组件。