C#如何给PDF文档添加注释

简介: 整理文档时,我们可能会需要在一些或一段文字上添加注释加以说明,那如何以编程的方式实现呢?本文将实例讲述C#中如何使用免费组件给PDF文档添加文本注释,包括自由文本注释。自由文本注释能允许我们自定义它的风格和外观,非常具有实用价值。

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

string text = "HelloWorld";

PointF point = new PointF(200, 100);

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

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

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

(page as PdfNewPage).Annotations.Add(annotation1);

doc.SaveToFile("result.pdf");

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

全部代码:

 1             PdfDocument doc = new PdfDocument();
 2 
 3             PdfPageBase page = doc.Pages.Add();
 4 
 5             PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
 6 
 7             string text = "HelloWorld";
 8 
 9             PointF point = new PointF(200, 100);
10 
11             page.Canvas.DrawString(text, font, PdfBrushes.Red, point);
12 
13  
14 
15             PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font);
16 
17             annotation1.Border = new PdfAnnotationBorder(0.75f);
18 
19             annotation1.TextMarkupColor = Color.Green;
20 
21             annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
22 
23             (page as PdfNewPage).Annotations.Add(annotation1);
24 
25             doc.SaveToFile("result.pdf");
26 
27             System.Diagnostics.Process.Start("result.pdf");
View Code

 

添加自由文本注释

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

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

PdfDocument doc = new PdfDocument();

PdfPageBase page = doc.Pages.Add();

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

RectangleF rect = new RectangleF(0, 40, 150, 50);

PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

textAnnotation.Text = "Free text annotation ";

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

步骤4添加注释到页面。

page.AnnotationsWidget.Add(textAnnotation); 

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

doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);

System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");

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

全部代码:

 1             PdfDocument doc = new PdfDocument();
 2 
 3             PdfPageBase page = doc.Pages.Add();
 4 
 5            
 6 
 7             RectangleF rect = new RectangleF(0, 40, 150, 50);
 8 
 9             PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
10 
11             textAnnotation.Text = "Free text annotation ";
12 
13         
14 
15             PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
16 
17             PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
18 
19             textAnnotation.Font = font;
20 
21             textAnnotation.Border = border;
22 
23             textAnnotation.BorderColor = Color. Purple;
24 
25             textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle;
26 
27             textAnnotation.Color = Color.Pink;
28 
29             textAnnotation.Opacity = 0.8f;
30 
31            
32 
33             page.AnnotationsWidget.Add(textAnnotation);
34 
35             doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
36 
37             System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
View Code

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

 

目录
相关文章
|
2月前
|
应用服务中间件
使用 Adobe Livecycle Enterprise service 将 word 文档转换成 PDF 格式
使用 Adobe Livecycle Enterprise service 将 word 文档转换成 PDF 格式
28 0
|
3月前
|
Ubuntu Java Linux
在Spring Boot中使用iTextPDF创建动态PDF文档
iTextPDF 是一个用于创建和操作 PDF(Portable Document Format)文档的流行的 Java 库。它提供了一套全面的功能,用于处理 PDF 文件,包括创建新文档、修改现有文档以及提取信息。
86 1
|
5月前
|
Python
python获取pdf和word文档页数
python获取pdf和word文档页数
145 0
|
6月前
|
设计模式 架构师 Java
阿里P8架构师都要学习研究的java加强版23种设计模式神级PDF文档
说在前面的话 Java作为老牌纯正的编程语言,在规范性上有着天然优势。因此本版的设计模式讲解全部用Java语言来描述,并针对Java语言的特性对讲解内容做了相当大的改动。 不知道大家是否听过编程界的一段话:掌握设计模式相当于华山派的"气宗",是程序员的内功修为,虽然在同样的学习时间下,类似Python这种"剑宗"的开发模式见效更快,但是长远来看,"气宗"才是走向软件架构师以上级别的必由之路。 所以,掌握气宗就掌握了编程命脉,然而学习设计模式有四大境界: 接下来给大家分享的就是java溢彩加强版大话设计模式包含的内容知识点。 总目录 主要内容 本文是百万销量的经典畅销书《
124 0
|
6月前
|
C#
C# .net webapi使用swagger时显示controller注释
C# .net webapi使用swagger时显示controller注释
86 0
|
1月前
|
数据采集 移动开发 前端开发
springboot使用html模版导出pdf文档
springboot使用html模版导出pdf文档
|
1月前
|
XML 程序员 编译器
C#注释
C#注释
18 0
|
2月前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
101 0
|
2月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
33 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
|
6月前
|
Cloud Native Go 开发者
使用WPS自动化转换办公文档: 将Word, PowerPoint和Excel文件转换为PDF
使用WPS自动化转换办公文档: 将Word, PowerPoint和Excel文件转换为PDF
102 0