本文将实例讲述C#中如何使用免费组件给PDF文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值
正文
使用C#给PDF文档添加注释的实现代码
本文将实例讲述C#中如何使用免费组件给PDF文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值
整理文档时,我们可能会需要在一些或一段文字上添加注释加以说明,那如何以编程的方式实现呢?本文将实例讲述c#中如何使用免费组件给pdf文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值。
首先,下载这个免费版组件free spire.pdf。组件下载安装后,visual studio创建c#控制台项目,添加bin文件夹的.dll作为引用以及以下命名空间:
|
1
2
3
4
5
6
|
using system;using system.drawing;using system.windows.forms;using spire.pdf;using spire.pdf.graphics;using spire.pdf.annotations; |
现在我们就来具体看看如何给新建的文档添加注释的。
步骤1:新建一个pdf文档对象,再添加一个新页面。
|
1
2
|
pdfdocument doc = new pdfdocument();pdfpagebase page = doc.pages.add(); |
步骤2:文档中添加文本,并设置文本的位置、字体大小、颜色。
|
1
2
3
4
|
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:给文本添加注释,并设置注释的边框、颜色及位置。
|
1
2
3
4
|
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:将注释添加到页面,最后保存文档。
|
1
2
|
(page as pdfnewpage).annotations.add(annotation1);doc.savetofile("result.pdf"); |
这是添加注释后的效果图:

全部代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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文档对象,并添加一个新页面。
|
1
2
|
pdfdocument doc = new pdfdocument();pdfpagebase page = doc.pages.add(); |
步骤2:初始化一个pdffreetextannotation,然后自定义注释的文本。
|
1
2
3
|
rectanglef rect = new rectanglef(0, 40, 150, 50);pdffreetextannotation textannotation = new pdffreetextannotation(rect);textannotation.text = "free text annotation "; |
步骤3:设置注释的属性,包括字体、填充颜色、边框颜色和透明度。
|
1
2
3
4
5
6
7
8
|
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:添加注释到页面。
|
1
|
page.annotationswidget.add(textannotation); |
步骤5:保存并重新打开文档。
|
1
2
|
doc.savetofile("freetextannotation.pdf", fileformat.pdf);system.diagnostics.process.start("freetextannotation.pdf"); |
这是添加自由文本注释的效果图:

全部代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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添加注释,也许对你有帮助。谢谢浏览!

发表评论