为了更改地让读者理解内容,有时候需要给文档添加注释。本文将介绍如何使用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<PdfTextFragment> 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];
PdfHighlightAnnotation annotation = new PdfHighlightAnnotation(page, rect);
annotation.Title = "管理员";
annotation.Contents = "成都冰蓝科技有限公司出品";
annotation.Border.Width = 0.2f;
annotation.Color = new PdfRGBColor(Color.Green);
PdfAnnotationCollection.Create(page).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 PdfHighlightAnnotation(page, rect)
annotation.Title = "管理员"
annotation.Contents = "成都冰蓝科技有限公司出品"
annotation.Border.Width = 0.2F
annotation.Color = New PdfRGBColor(Color.Green)
PdfAnnotationCollection.Create(page).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<PdfTextFragment> 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, 20, 20);
PdfTextAnnotation parent = new PdfTextAnnotation(page, rect);
PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(page, rect);
popupAnnotation.Parent = parent;
parent.Popup = popupAnnotation;
//指定附注的文本、图标及图标颜色(附注文本存放在父注释上)
parent.Contents = "Adobe公司旗下产品,用于创建编辑PDF文档。";
parent.Icon = PdfTextIcon.Note;
parent.Color = Color.Yellow;
//添加附注到PDF
PdfAnnotationCollection.Create(page).Add(parent);
PdfAnnotationCollection.Create(page).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, 20, 20)
Dim parent As New PdfTextAnnotation(page, rect)
Dim popupAnnotation As New PdfPopupAnnotation(page, rect)
popupAnnotation.Parent = parent
parent.Popup = popupAnnotation
'指定附注的文本、图标及图标颜色(附注文本存放在父注释上)
parent.Contents = "Adobe公司旗下产品,用于创建编辑PDF文档。"
parent.Icon = PdfTextIcon.Note
parent.Color = Color.Yellow
'添加附注到PDF
PdfAnnotationCollection.Create(page).Add(parent)
PdfAnnotationCollection.Create(page).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<PdfTextFragment> 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);
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", 10f), true);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(page, rect, new PdfDefaultAppearance(font));
//指定注释文字及字体,注释框边界颜色及背景色
textAnnotation.Contents = "什么是Spire.PDF?";
textAnnotation.Border.Width = 0.5f;
textAnnotation.Border.Color = Color.Purple;
textAnnotation.Color = Color.Yellow;
textAnnotation.StrokingOpacity = 0.8f;
//添加注释到PDF
PdfAnnotationCollection.Create(page).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 font As New PdfTrueTypeFont(New Font("宋体", 10.0F), True)
Dim textAnnotation As New PdfFreeTextAnnotation(page, rect, New PdfDefaultAppearance(font))
'指定注释文字及字体,注释框边界颜色及背景色
textAnnotation.Contents = "什么是Spire.PDF?"
textAnnotation.Border.Width = 0.5F
textAnnotation.Border.Color = Color.Purple
textAnnotation.Color = Color.Yellow
textAnnotation.StrokingOpacity = 0.8F
'添加注释到PDF
PdfAnnotationCollection.Create(page).Add(textAnnotation)
'保存文档
doc.SaveToFile("文本框注释.pdf")








