class="MsoNormal">向文档添加注释,是一种比较常用的向读者传递某些重要信息的手段。通过编程的方式来添加PDF注释,我们可以自定义注释的外观、类型及其他一些个性化的设置,这种可供选择的操作在编程中提供了更多的实用性。因此,接下来,本文将介绍添加几种不同类型的PDF注释的方法。主要包含以下5种:
?
工具使用
using Spire.Pdf; using Spire.Pdf.General.Find; using System.Drawing; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; namespace Annotation_PDF { class Program { static void Main(string[] args) { //初始化PdfDocument类实例,并加载测试文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //获取第一页 PdfPageBase page = doc.Pages[0]; //调用方法FindText()查找需要添加注释的字符串 PdfTextFind[] results = page.FindText("IPCC").Finds; //指定注释添加的位置 float x = results[0].Position.X - doc.PageSettings.Margins.Top; float y = results[0].Position.Y - doc.PageSettings.Margins.Left + results[0].Size.Height - 23; //创建弹出式注释 RectangleF rect = new RectangleF(x, y, 10, 0); PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect); //添加注释内容,并设置注释的图标类型和颜色 popupAnnotation.Text = "IPCC,This is a scientific and intergovernmental body under the auspices of the United Nations."; popupAnnotation.Icon = PdfPopupIcon.Help; popupAnnotation.Color = Color.DarkOliveGreen; //添加注释到文件 page.AnnotationsWidget.Add(popupAnnotation); //保存并打开文档 doc.SaveToFile("Annotation.pdf"); System.Diagnostics.Process.Start("Annotation.pdf"); } } }
?在选择注释标签类型时,有以下几种类型可供选择:
?
?
?
using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace FreeTextAnnotation_pdf { class Program { static void Main(string[] args) { //创建PdfDocument类对象,加载测试文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); PdfPageBase page = doc.Pages[0]; //初始化RectangleF类,指定注释添加的位置、注释图标大小 RectangleF rect = new RectangleF(50, 500, 100, 40); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); ////添加注释内容 textAnnotation.Text = "This is just a sample, please refer the original article to see more!"; //设置注释属性,包括字体、字号、注释边框粗细、边框颜色、填充颜色等 PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 9); PdfAnnotationBorder border = new PdfAnnotationBorder(0.75f); textAnnotation.Font = font; textAnnotation.Border = border; textAnnotation.BorderColor = Color.White; textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle; textAnnotation.Color = Color.Transparent; textAnnotation.Opacity = 0.8f; //添加注释到页面 page.AnnotationsWidget.Add(textAnnotation); //保存并打开文档 doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("FreeTextAnnotation.pdf"); } } }
?
?添加效果:
?
?
?
using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace FreeTextAnnotation_pdf { class Program { static void Main(string[] args) { //创建PdfDocument类对象,加载测试文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); PdfPageBase page = doc.Pages[0]; //初始化RectangleF类,指定注释添加的位置、注释图标大小 RectangleF rect = new RectangleF(50, 500, 100, 40); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); //添加注释内容 textAnnotation.Text = "This is just a sample, Click here to read the original file!"; //设置注释属性,包括字体、字号、注释边框粗细、边框颜色、填充颜色等 PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 9); PdfAnnotationBorder border = new PdfAnnotationBorder(0.75f); textAnnotation.Font = font; textAnnotation.Border = border; textAnnotation.BorderColor = Color.White; textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle; textAnnotation.Color = Color.Transparent; //添加需要链接到的文件地址,并添加链接到注释 string filePath = @"C:\Users\Administrator\Desktop\original.pdf"; PdfFileLinkAnnotation link = new PdfFileLinkAnnotation(rect, filePath); page.AnnotationsWidget.Add(link); //添加注释到页面 page.AnnotationsWidget.Add(textAnnotation); //保存并打开文档 doc.SaveToFile("LinkAnnotation.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("LinkAnnotation.pdf"); } } }
?
添加效果:
?
?
using Spire.Pdf; using Spire.Pdf.Annotations; using System; using System.Drawing; namespace PolygonAnnotation_PDF { class Program { static void Main(string[] args) { //创建PdfDocument类对象,加载测试文档 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("sample.pdf"); //获取文档第一页 PdfPageBase page = pdf.Pages[0]; //实例化PdfPolygonAnnotation类,指定多边形各顶点位置 PdfPolygonAnnotation polygon = new PdfPolygonAnnotation(page, new PointF[] { new PointF(0, 30), new PointF(30, 15), new PointF(60, 30), new PointF(45, 50), new PointF(15, 50), new PointF(0, 30)}); //指定多边形注释的边框颜色、注释内容、作者、边框类型、修订时间等属性 polygon.Color = Color.CornflowerBlue; polygon.Text = "This article is created by Mia, permit read ONLY."; polygon.Author = "Editor's Note"; polygon.Subject = "polygon annotation demo"; polygon.BorderEffect = PdfBorderEffect.BigCloud; polygon.ModifiedDate = DateTime.Now; //添加注释到页面 page.AnnotationsWidget.Add(polygon); //保存并打开文档 pdf.SaveToFile("Polygon_Annotation.pdf"); System.Diagnostics.Process.Start("Polygon_Annotation.pdf"); } } }
?
添加效果:
?
using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Fields; using Spire.Pdf.Graphics; using System.Drawing; namespace LineAnnotation_PDF { class Program { static void Main(string[] args) { //实例化PdfDocument类,加载文档 PdfDocument document = new PdfDocument(); document.LoadFromFile("sample.pdf"); PdfPageBase page = document.Pages[0]; //在页面指定位置绘制Line类型注释,并添加注释的文本内容 int[] linePoints = new int[] { 100,300, 180, 300 }; PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(linePoints, "Comment Text"); //设置线条粗细、指向 lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid; lineAnnotation.lineBorder.BorderWidth = 1; lineAnnotation.LineIntent = PdfLineIntent.LineDimension; //设置线性注释的头、尾形状、标记的类型 lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Circle; lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond; lineAnnotation.Flags = PdfAnnotationFlags.Default; //设置注释颜色 lineAnnotation.InnerLineColor = new PdfRGBColor(Color.Green); lineAnnotation.BackColor = new PdfRGBColor(Color.Green); lineAnnotation.LeaderLineExt = 0; lineAnnotation.LeaderLine = 0; //添加注释到页面 page.AnnotationsWidget.Add(lineAnnotation); //保存并打开文档 document.SaveToFile("LineAnnotation.pdf"); System.Diagnostics.Process.Start("LineAnnotation.pdf"); } } }
?
效果图:
?
?(本文完)
?