• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > 使用C#给PDF文档添加注释的实现代码

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c语言括号的使用,罗技c930摄像头使用,sony闹钟c218使用,中诺c256使用说明书,索尼2500c使用教程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

整理文档时,我们可能会需要在一些或一段文字上添加注释加以说明,那如何以编程的方式实现呢?本文将实例讲述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;
</div>

现在我们就来具体看看如何给新建的文档添加注释的。

步骤1:新建一个PDF文档对象,再添加一个新页面。

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
</div>

步骤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);
</div>

步骤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);
</div>

步骤4:将注释添加到页面,最后保存文档。

(page as PdfNewPage).Annotations.Add(annotation1);
doc.SaveToFile("result.pdf");
</div>

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

全部代码:

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");
</div>

添加自由文本注释

同样,给文档添加自由文本注释也相对简单。

步骤1:新建一个PDF文档对象,并添加一个新页面。

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
</div>

步骤2:初始化一个PdfFreeTextAnnotation,然后自定义注释的文本。

RectangleF rect = new RectangleF(0, 40, 150, 50);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
textAnnotation.Text = "Free text annotation ";
</div>

步骤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;
</div>

步骤4:添加注释到页面。

page.AnnotationsWidget.Add(textAnnotation); 
</div>

步骤5:保存并重新打开文档。

doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
</div>

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

全部代码:

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");
</div>

之前我也分享过如何在C#里面给PPT添加注释,也许对你有帮助。谢谢浏览!

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 使用C#编写15子游戏
  • 使用C#正则表达式获取必应每日图片地址
  • 使用C#开发ActiveX控件
  • 使用C#给PDF文档添加注释的实现代码
  • 使用C#语言实现的查询条件界面展开和收起功能
  • 使用C#发送Http请求实现模拟登陆实例
  • 使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
  • 使用C#编写简单的图形化的可发送附件的邮件客户端程序
  • 使用C#发送带附件的电子邮件的方法的代码示例分析
  • 使用C#写了一个可以推算火车票身份证号码的小程序

相关文章

  • 2017-05-28C# 获取系统进程的用户名
  • 2017-05-28C#动态生成按钮及定义按钮事件的方法
  • 2017-05-28c#使用htmlagilitypack解析html格式字符串
  • 2017-05-28C#泛型和反射实例解析
  • 2017-05-28C#中重载相等(==)运算符示例
  • 2017-05-28详解二维码生成工厂
  • 2017-05-28c#不使用系统api实现可以指定区域屏幕截屏功能
  • 2017-05-28C#读写INI文件的方法
  • 2017-05-28c#使用多线程的几种方式示例详解
  • 2017-05-28C#中String与string的区别分析

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 基于WebRequest.RegisterPrefix的使用详解
    • C#操作本地文件及保存文件到数据库的基本方法总结
    • .NET垃圾回收器(GC)原理浅析
    • C# 泛型深入理解介绍
    • 使用C#给PDF文档添加注释的实现代码
    • C#反射在实际应用中的实例代码
    • c#生成站点地图(SiteMapPath)文件示例程序
    • C#使用SqlBulkCopy批量复制数据到数据表
    • C#多线程经典示例(吃苹果)
    • DevExpress之ChartControl创建Drill-Down样式的Title实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有