为了更改地让读者理解内容,有时候需要给文档添加注释。本文将介绍如何使用Spire.PDF为PDF文档添加高亮、附注和文本框注释。
高亮文本标记
//初始化PdfDocument实例并加载现有文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Spire.PDF介绍.pdf");
//获取第一页
PdfPageBase page = doc.Pages[0];
//获取文本
PdfTextFind[] results = page.FindText("Spire.PDF for .NET").Finds;
//创建注释,指定作者、注释文本、高亮颜色等信息
PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("管理员", "成都冰蓝科技有限公司出品", results[0].SearchText, results[0].Position, results[0].Font);
annotation.Border = new PdfAnnotationBorder(0.2f);
annotation.TextMarkupColor = Color.Green;
//添加注释到PDF
page.AnnotationsWidget.Add(annotation);
//保存文档
doc.SaveToFile("高亮文本.pdf");
弹出式附注
//初始化PdfDocument实例并加载现有文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Spire.PDF介绍.pdf");
//获取第一页
PdfPageBase page = doc.Pages[0];
//获取文本
PdfTextFind[] results = page.FindText("Acrobat").Finds;
//指定注释的位置
float x = results[0].Position.X - doc.PageSettings.Margins.Left + results[0].Size.Width + 5;
float y = results[0].Position.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.AnnotationsWidget.Add(popupAnnotation);
//保存文档
doc.SaveToFile("弹出式附注.pdf");
文本框注释
若文本框注释含有中日韩字体,在使用Adobe打开该PDF时, 需要安装亚洲语言资源文件。
//初始化PdfDocument实例并加载现有文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Spire.PDF介绍.pdf");
//获取第一页
PdfPageBase page = doc.Pages[0];
//获取文本
PdfTextFind[] results = page.FindText("Acrobat").Finds;
//指定文本框注释的位置
float x = results[0].Position.X - doc.PageSettings.Margins.Left + results[0].Size.Width + 20;
float y = results[0].Position.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("Arial Unicode MS", 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.AnnotationsWidget.Add(textAnnotation);
//保存文档
doc.SaveToFile("文本框注释.pdf");