-
C#如何给PDF文档添加注释
整理文档时,我们可能会需要在一些或一段文字上添加注释加以说明,那如何以编程的方式实现呢?本文将实例讲述C#中如何使用免费组件给PDF文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值。
首先,下载这个免费版组件Free Spire.PDF https://www.e-iceblue.com/Introduce/free-pdf-component.html。组件下载安装后,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");
这是添加注释后的效果图:
全部代码:

1 PdfDocument doc = new PdfDocument(); 2 3 PdfPageBase page = doc.Pages.Add(); 4 5 PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13); 6 7 string text = "HelloWorld"; 8 9 PointF point = new PointF(200, 100); 10 11 page.Canvas.DrawString(text, font, PdfBrushes.Red, point); 12 13 14 15 PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font); 16 17 annotation1.Border = new PdfAnnotationBorder(0.75f); 18 19 annotation1.TextMarkupColor = Color.Green; 20 21 annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left); 22 23 (page as PdfNewPage).Annotations.Add(annotation1); 24 25 doc.SaveToFile("result.pdf"); 26 27 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");
这是添加自由文本注释的效果图:
全部代码:

1 PdfDocument doc = new PdfDocument(); 2 3 PdfPageBase page = doc.Pages.Add(); 4 5 6 7 RectangleF rect = new RectangleF(0, 40, 150, 50); 8 9 PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); 10 11 textAnnotation.Text = "Free text annotation "; 12 13 14 15 PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10); 16 17 PdfAnnotationBorder border = new PdfAnnotationBorder(1f); 18 19 textAnnotation.Font = font; 20 21 textAnnotation.Border = border; 22 23 textAnnotation.BorderColor = Color. Purple; 24 25 textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle; 26 27 textAnnotation.Color = Color.Pink; 28 29 textAnnotation.Opacity = 0.8f; 30 31 32 33 page.AnnotationsWidget.Add(textAnnotation); 34 35 doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF); 36 37 System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
原文链接:https://www.cnblogs.com/Yesi/p/6292994.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数