服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 使用C#给PDF文档添加注释的实现代码

使用C#给PDF文档添加注释的实现代码

2021-12-21 16:18C#教程网 C#

本文将实例讲述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");

这是添加注释后的效果图:

使用C#给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");

这是添加自由文本注释的效果图:

使用C#给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添加注释,也许对你有帮助。谢谢浏览!

延伸 · 阅读

精彩推荐
  • C#深入解析C#中的交错数组与隐式类型的数组

    深入解析C#中的交错数组与隐式类型的数组

    这篇文章主要介绍了深入解析C#中的交错数组与隐式类型的数组,隐式类型的数组通常与匿名类型以及对象初始值设定项和集合初始值设定项一起使用,需要的...

    C#教程网6172021-11-09
  • C#C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    C#设计模式之Visitor访问者模式解决长隆欢乐世界问题实例

    这篇文章主要介绍了C#设计模式之Visitor访问者模式解决长隆欢乐世界问题,简单描述了访问者模式的定义并结合具体实例形式分析了C#使用访问者模式解决长...

    GhostRider9502022-01-21
  • C#WPF 自定义雷达图开发实例教程

    WPF 自定义雷达图开发实例教程

    这篇文章主要介绍了WPF 自定义雷达图开发实例教程,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下...

    WinterFish13112021-12-06
  • C#C#通过KD树进行距离最近点的查找

    C#通过KD树进行距离最近点的查找

    这篇文章主要为大家详细介绍了C#通过KD树进行距离最近点的查找,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    帆帆帆6112022-01-22
  • C#Unity3D实现虚拟按钮控制人物移动效果

    Unity3D实现虚拟按钮控制人物移动效果

    这篇文章主要为大家详细介绍了Unity3D实现虚拟按钮控制人物移动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    shenqingyu060520232410972022-03-11
  • C#C#实现XML文件读取

    C#实现XML文件读取

    这篇文章主要为大家详细介绍了C#实现XML文件读取的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Just_for_Myself6702022-02-22
  • C#C#裁剪,缩放,清晰度,水印处理操作示例

    C#裁剪,缩放,清晰度,水印处理操作示例

    这篇文章主要为大家详细介绍了C#裁剪,缩放,清晰度,水印处理操作示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    吴 剑8332021-12-08
  • C#C# 实现对PPT文档加密、解密及重置密码的操作方法

    C# 实现对PPT文档加密、解密及重置密码的操作方法

    这篇文章主要介绍了C# 实现对PPT文档加密、解密及重置密码的操作方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    E-iceblue5012022-02-12