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

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# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08