使用C#给PDF文档添加注释的实现代码
整理文档时,我们可能会需要在一些或一段文字上添加注释加以说明,那如何以编程的方式实现呢?本文将实例讲述C#中如何使用免费组件给PDF文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值。
首先,下载这个免费版组件Free Spire.PDF。组件下载安装后,Visual Studio创建C#控制台项目,添加bin文件夹的.DLL作为引用以及以下命名空间:
using System; using System.Drawing; using System.Windows.Forms; using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Annotations;
现在我们就来具体看看如何给新建的文档添加注释的。
步骤1:新建一个PDF文档对象,再添加一个新页面。
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
步骤2:文档中添加文本,并设置文本的位置、字体大小、颜色。
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13); string text = "HelloWorld"; PointF point = new PointF(200, 100); page.Canvas.DrawString(text, font, PdfBrushes.Red, point);
步骤3:给文本添加注释,并设置注释的边框、颜色及位置。
PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font); annotation1.Border = new PdfAnnotationBorder(0.75f); annotation1.TextMarkupColor = Color.Green; annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
步骤4:将注释添加到页面,最后保存文档。
(page as PdfNewPage).Annotations.Add(annotation1); doc.SaveToFile("result.pdf");
这是添加注释后的效果图:
全部代码:
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13); string text = "HelloWorld"; PointF point = new PointF(200, 100); page.Canvas.DrawString(text, font, PdfBrushes.Red, point); PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font); annotation1.Border = new PdfAnnotationBorder(0.75f); annotation1.TextMarkupColor = Color.Green; annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left); (page as PdfNewPage).Annotations.Add(annotation1); doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
添加自由文本注释
同样,给文档添加自由文本注释也相对简单。
步骤1:新建一个PDF文档对象,并添加一个新页面。
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
步骤2:初始化一个PdfFreeTextAnnotation,然后自定义注释的文本。
RectangleF rect = new RectangleF(0, 40, 150, 50); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); textAnnotation.Text = "Free text annotation ";
步骤3:设置注释的属性,包括字体、填充颜色、边框颜色和透明度。
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10); PdfAnnotationBorder border = new PdfAnnotationBorder(1f); textAnnotation.Font = font; textAnnotation.Border = border; textAnnotation.BorderColor = Color. Purple; textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle; textAnnotation.Color = Color. Pink; textAnnotation.Opacity = 0.8f;
步骤4:添加注释到页面。
page.AnnotationsWidget.Add(textAnnotation);
步骤5:保存并重新打开文档。
doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
这是添加自由文本注释的效果图:
全部代码:
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); RectangleF rect = new RectangleF(0, 40, 150, 50); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); textAnnotation.Text = "Free text annotation "; PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10); PdfAnnotationBorder border = new PdfAnnotationBorder(1f); textAnnotation.Font = font; textAnnotation.Border = border; textAnnotation.BorderColor = Color. Purple; textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle; textAnnotation.Color = Color.Pink; textAnnotation.Opacity = 0.8f; page.AnnotationsWidget.Add(textAnnotation); doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
之前我也分享过如何在C#里面给PPT添加注释,也许对你有帮助。谢谢浏览!
您可能感兴趣的文章
- 01-10C#使用Dispose模式实现手动对资源的释放
- 01-10C#3.0使用EventLog类写Windows事件日志的方法
- 01-10C#使用windows服务开启应用程序的方法
- 01-10c# ArrayList的使用方法小总结
- 01-10C#使用ADO.Net部件来访问Access数据库的方法
- 01-10C#使用Mutex简单实现程序单实例运行的方法
- 01-10C#获取动态生成的CheckBox值
- 01-10使用Nopcommerce为商城添加满XX减XX优惠券功能
- 01-10C#实现HTML转WORD及WORD转PDF的方法
- 01-10C#中yield用法使用说明
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10C#中split用法实例总结
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载