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

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

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

PdfDocument doc = new PdfDocument();

PdfPageBase page = doc.Pages.Add();

步骤2文档中添加文本,并设置文本的位置、字体大小、颜色。

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, );

string text = "HelloWorld";

PointF point = new PointF(, );

page.Canvas.DrawString(text, font, PdfBrushes.Red, point);

步骤3给文本添加注释,并设置注释的边框、颜色及位置。

PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(, ), 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");

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

全部代码:

             PdfDocument doc = new PdfDocument();

             PdfPageBase page = doc.Pages.Add();

             PdfFont font = new PdfFont(PdfFontFamily.Helvetica, );

             string text = "HelloWorld";

             PointF point = new PointF(, );

             page.Canvas.DrawString(text, font, PdfBrushes.Red, point);

             PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(, ), 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文档对象,并添加一个新页面。

PdfDocument doc = new PdfDocument();

PdfPageBase page = doc.Pages.Add();

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

RectangleF rect = new RectangleF(, , , );

PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

textAnnotation.Text = "Free text annotation ";

步骤3设置注释的属性,包括字体、填充颜色、边框颜色和透明度。

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, );

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

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

全部代码:

             PdfDocument doc = new PdfDocument();

             PdfPageBase page = doc.Pages.Add();

             RectangleF rect = new RectangleF(, , , );

             PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

             textAnnotation.Text = "Free text annotation ";

             PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, );

             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#如何给PDF文档添加注释的更多相关文章

  1. C#给PDF文档添加文本和图片页眉

    页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码.日期.公司徽标.文档标题.文件名或作者名等等.那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件 ...

  2. C# 给现有PDF文档添加页眉、页脚

    概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...

  3. 如何给PDF文档添加和删除贝茨编号

    PDF文件的使用频率高了,我们也不只局限于使用PDF文件了,也会需要编辑PDF文件的时候,那么如何在PDF文件中添加和去除贝茨编号呢,应该有很多小伙伴都想知道吧,今天就来跟大家分享一下吧,小伙伴们就一 ...

  4. 利用iTextSharp组件给PDF文档添加图片水印,文字水印

    最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了.以下做个记录: 首先会用到iTextSharp组件,大家可以去官网下载,同时我也会在本文中附加进来. 代码中添加引用为:   using S ...

  5. ABBYY FineReader 15 如何为PDF文档添加页眉页脚

    页眉.页脚是文档页面顶部或底部重复出现的文本信息.很多用户会习惯在文档页面的顶部与底部区域添加页眉.页脚来展现页码.文档标题.作者姓名.品牌名称等附加信息.而ABBYY FineReader 15(W ...

  6. Java 插入附件到PDF文档

    在文档中插入附件,可以起到与源文档配套使用的目的,以一种更简便的方式对文档起到补充说明的作用.下面将介绍通过Java编程插入附件到PDF文档中的方法.这里插入的文档可以是常见的文档类型,如Word.E ...

  7. PDF文档小技巧整理一览

    1.福昕阅读器文档背景修改为保护眼睛的颜色? 1)文件 -> 偏好设置 -> 访问 -> 勾选 "改变文档颜色" 2)选择 '自定义颜色'->'页面背景颜色 ...

  8. C# 给PDF文档设置过期时间

    我们可以给一些重要文档或者临时文件设置过期时间和过期信息提示来提醒读者或管理者文档的时效性,并及时对文档进行调整.更新等.下面,分享通过C#程序代码来给PDF文档设置过期时间的方法. 引入dll程序集 ...

  9. opencart 3添加pdf文档下载功能

    opencart 3适合做外贸商城,如果能在产品页那边添加pdf文档功能是最好的,符合国外用户的使用习惯,增加客户的黏性.其实opencart已经有一个downloadable product可下载产 ...

随机推荐

  1. ecos的model

    表->dbschema->model 虚拟化model机制 在dbschema存在model不存在的情况下 很多mvc结构都这么来 model命名规则 {$app_name}_mdl_{$ ...

  2. springMVC和spring上下文的关系

    springMVC继承了spring的servletcontext上下文, 所以, controller里的@Resource注入可以用以下替代 @Resource private IUserServ ...

  3. c语言基本数据类型short、int、long、char、float、double

    C 语言包含的数据类型如下图所示: 一.数据类型与“模子”short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 怎么去理解它们呢? 举个例子 ...

  4. Android学习笔记之Intent(2)

    打开网页 package com.jiahemeikang.helloandroid; import java.io.File; import com.jiahemikang.service.Echo ...

  5. MOS管常识

    http://anlx27.iteye.com/blog/1583089 学过模拟电路,但都忘得差不多了.重新学习MOS管相关知识,大多数是整理得来并非原创.如有错误还请多多指点! 先上一张图 一. ...

  6. AsParallel 用法

    http://www.cnblogs.com/leslies2/archive/2012/02/08/2320914.html AsParallel 通常想要实现并行查询,只需向数据源添加 AsPar ...

  7. Docker Swarm集群

    Docker Swarm集群 IP 10.6.17.11  管理节点 IP 10.6.17.12   节点A IP 10.6.17.13   节点B IP 10.6.17.14   节点C 安装 Sw ...

  8. nginx location配置(URL)

    语法规则: location [=|~|~*|^~] /uri/ { … }= 表示精确匹配,这个优先级也是最高的^~ 表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url ...

  9. POJ3259负环判定

    题意:有n个顶点,m条边,然后有w个洞,过每个洞的时间为-ti,求是否会时光倒流 分析:就是求是否存在负圈,用Bellman-Floyd判定是否存在负圈即可,注意是无向图,所以路径是双向可达的 #in ...

  10. 冯如杯day1

    day1 今天尝试配置了seetaface工程 主要按照这个网址中提示的步骤配置seetaface 第一步安装并配置OpenCV 按照这个网址进行配置,结果遇到了这样的错误 无法启动此程序,因为计算机 ...