为了更改地让读者理解内容,有时候需要给文档添加注释。本文将介绍如何使用Spire.PDF为PDF文档添加高亮、附注和文本框注释。
高亮文本标记
C#
//初始化PdfDocument实例并加载现有文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(""Spire.PDF介绍.pdf"");
//获取第一页
PdfPageBase page = doc.Pages[0];
//获取文本
PdfTextFinder finder = new PdfTextFinder(page);
finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;
List results = finder.Find(""Spire.PDF for .NET"");
PdfTextFragment textFragment = results[0];
for (int i = 0; i < textFragment.Bounds.Length; i++)
{
RectangleF rect = textFragment.Bounds[i];
PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation(""管理员"", ""成都冰蓝科技有限公司出品"", rect);
annotation.Border = new PdfAnnotationBorder(0.2f);
annotation.TextMarkupColor = Color.Green;
page.Annotations.Add(annotation);
}
//保存文档
doc.SaveToFile(""高亮文本.pdf"");
VB.NET
'初始化PdfDocument实例并加载现有文档
Dim doc As New PdfDocument()
doc.LoadFromFile(""Spire.PDF介绍.pdf"")
'获取第一页
Dim page As PdfPageBase = doc.Pages(0)
' 获取文本
Dim finder As New PdfTextFinder(page)
finder.Options.Parameter = TextFindParameter.WholeWord
Dim results As List(Of PdfTextFragment) = finder.Find(""Spire.PDF for .NET"")
Dim textFragment As PdfTextFragment = results(0)
'创建注释,指定作者、注释文本、高亮颜色等信息
For i As Integer = 0 To textFragment.Bounds.Length - 1
Dim rect As RectangleF = textFragment.Bounds(i)
Dim annotation As New PdfTextMarkupAnnotation(""管理员"", ""成都冰蓝科技有限公司出品"", rect)
annotation.Border = New PdfAnnotationBorder(0.2F)
annotation.TextMarkupColor = Color.Green
page.Annotations.Add(annotation)
Next
'保存文档
doc.SaveToFile(""高亮文本.pdf"")

弹出式附注
C#
//初始化PdfDocument实例并加载现有文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(""Spire.PDF介绍.pdf"");
//获取第一页
PdfPageBase page = doc.Pages[0];
//获取文本
PdfTextFinder finder = new PdfTextFinder(page);
finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;
List results = finder.Find(""Spire.PDF for .NET"");
//指定注释的位置
float x = results[0].Bounds[0].X - doc.PageSettings.Margins.Left + results[0].Bounds[0].Width + 5;
float y = results[0].Bounds[0].Y - doc.PageSettings.Margins.Top;
//创建弹出式附注
RectangleF rect = new RectangleF(x, y, 0, 0);
PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect);
//指定附注的文本、图标及图标颜色
popupAnnotation.Text = ""Adobe公司旗下产品,用于创建编辑PDF文档。"";
popupAnnotation.Icon = PdfPopupIcon.Note;
popupAnnotation.Color = Color.Yellow;
//添加附注到PDF
page.Annotations.Add(popupAnnotation);
//保存文档
doc.SaveToFile(""弹出式附注.pdf"");
VB.NET
'初始化PdfDocument实例并加载现有文档
Dim doc As New PdfDocument()
doc.LoadFromFile(""Spire.PDF介绍.pdf"")
'获取第一页
Dim page As PdfPageBase = doc.Pages(0)
'获取文本
Dim finder As New PdfTextFinder(page)
finder.Options.Parameter = TextFindParameter.WholeWord
Dim results As List(Of PdfTextFragment) = finder.Find(""Spire.PDF for .NET"")
'指定注释的位置
Dim x As Single = results(0).Bounds(0).X - doc.PageSettings.Margins.Left + results(0).Bounds(0).Width + 5
Dim y As Single = results(0).Bounds(0).Y - doc.PageSettings.Margins.Top
'创建弹出式附注
Dim rect As New RectangleF(x, y, 0, 0)
Dim popupAnnotation As New PdfPopupAnnotation(rect)
'指定附注的文本、图标及图标颜色
popupAnnotation.Text = ""Adobe公司旗下产品,用于创建编辑PDF文档。""
popupAnnotation.Icon = PdfPopupIcon.Note
popupAnnotation.Color = Color.Yellow
'添加附注到PDF
page.Annotations.Add(popupAnnotation)
'保存文档
doc.SaveToFile(""弹出式附注.pdf"")

文本框注释
若文本框注释含有中日韩字体,在使用Adobe打开该PDF时, 需要安装亚洲语言资源文件。
C#
//初始化PdfDocument实例并加载现有文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(""Spire.PDF介绍.pdf"");
//获取第一页
PdfPageBase page = doc.Pages[0];
//获取文本
PdfTextFinder finder = new PdfTextFinder(page);
finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;
List results = finder.Find(""Acrobat"");
//指定文本框注释的位置
float x = results[0].Bounds[0].X - doc.PageSettings.Margins.Left + results[0].Bounds[0].Width + 20;
float y = results[0].Bounds[0].Y - doc.PageSettings.Margins.Top;
//创建文本框注释
RectangleF rect = new RectangleF(x, y, 100, 15);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
//指定注释文字及字体,注释框边界颜色及背景色
textAnnotation.Text = ""什么是Spire.PDF?"";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(""宋体"", 10f), true);
textAnnotation.Font = font;
PdfAnnotationBorder border = new PdfAnnotationBorder(0.5f);
textAnnotation.Border = border;
textAnnotation.BorderColor = Color.Purple;
textAnnotation.Color = Color.Yellow;
textAnnotation.Opacity = 0.8f;
//添加注释到PDF
page.Annotations.Add(textAnnotation);
//保存文档
doc.SaveToFile(""文本框注释.pdf"");
VB.NET
'初始化PdfDocument实例并加载现有文档
Dim doc As New PdfDocument()
doc.LoadFromFile(""Spire.PDF介绍.pdf"")
'获取第一页
Dim page As PdfPageBase = doc.Pages(0)
'获取文本
Dim finder As New PdfTextFinder(page)
finder.Options.Parameter = TextFindParameter.WholeWord
Dim results As List(Of PdfTextFragment) = finder.Find(""Acrobat"")
'指定文本框注释的位置
Dim x As Single = results(0).Bounds(0).X - doc.PageSettings.Margins.Left + results(0).Bounds(0).Width + 20
Dim y As Single = results(0).Bounds(0).Y - doc.PageSettings.Margins.Top
'创建文本框注释
Dim rect As New RectangleF(x, y, 100, 15)
Dim textAnnotation As New PdfFreeTextAnnotation(rect)
'指定注释文字及字体,注释框边界颜色及背景色
textAnnotation.Text = ""什么是Spire.PDF?""
Dim font As New PdfTrueTypeFont(New Font(""宋体"", 10.0F), True)
textAnnotation.Font = font
Dim border As New PdfAnnotationBorder(0.5F)
textAnnotation.Border = border
textAnnotation.BorderColor = Color.Purple
textAnnotation.Color = Color.Yellow
textAnnotation.Opacity = 0.8F
'添加注释到PDF
page.Annotations.Add(textAnnotation)
'保存文档
doc.SaveToFile(""文本框注释.pdf"")








