C# 在PDF中添加墨迹注释Ink Annotation的步骤详解
作者:E-iceblue 时间:2022-01-31 14:23:29
PDF中的墨迹注释(Ink Annotation),表现为徒手涂鸦式的形状;该类型的注释,可任意指定形状顶点的位置及个数,通过指定的顶点,程序将连接各点绘制成平滑的曲线。下面,通过C#程序代码介绍如何在PDF中添加该注释。
一、dll引用
步骤1:在Visual Studio中打开“解决方案资源管理器”- 鼠标右键点击“引用”-“管理NuGet包”。
步骤2:选择“浏览”-在搜索框中输入搜索内容,选择搜索结果,点击“安装”。
步骤3:依次点击“OK”-"接受",然后等待程序完成安装。
或者,通过官方渠道,下载包Spire.PDF for .NET到本地。解压后,将BIN文件夹下的Spire.Pdf.dll文件引用至VS程序。
二、代码示例
添加注释时,除了自定义各个点的位置及数量,也可以设置墨迹颜色、线条宽度、透明度、注释的内容、名称等。下面是代码实现的步骤:
创建PdfDocument类的对象,并通过PdfDocument.LoadFromFile(String fileName)方法加载PDF文档。
通过PdfDocument.Pages[int Index]属性获取PDF指定页面。
创建类型为int的对象集合,集合元素为各墨迹顶点。
创建PdfInkAnnotation类的实例。并通过该类提供的属性设置墨迹颜色、宽度、注释内容等格式。
调用PdfPageBase.AnnotationsWidget属性提供的PdfAnnotationCollection.Add(PdfAnnotation annotation)方法添加注释到PDF。
最后,通过PdfDocument.SaveToFile(string filename, FileFormat fileFormat)方法保存PDF文档到指定路径。
C#
using Spire.Pdf;
using Spire.Pdf.Annotations;
using System.Collections.Generic;
using System.Drawing;
namespace InkAnnotation
{
class Program
{
static void Main(string[] args)
{
//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("test.pdf");
//获取第一页
PdfPageBase pdfPage = pdf.Pages[0];
//设置墨迹坐标点位置
List<int[]> inkList = new List<int[]>();
int[] intPoints = new int[]
{
370,700,
120,720,
110,760,
220,800,
270,790,
350,770,
350,670
};
inkList.Add(intPoints);
//添加墨迹注释到PDF页面
PdfInkAnnotation inkannotation = new PdfInkAnnotation(inkList);
inkannotation.Color = Color.MediumVioletRed;
inkannotation.Border.Width = 6;
inkannotation.Opacity = 0.5f;
inkannotation.Text = "This is an ink annotation. ";
inkannotation.Name = "Manager";
pdfPage.AnnotationsWidget.Add(inkannotation);
//保存文档
Pdf.SaveToFile("AddInkAnnotation.pdf",FileFormat.PDF);
System.Diagnostics.Process.Start("AddInkAnnotation.pdf");
}
}
}
vb.net
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports System.Collections.Generic
Imports System.Drawing
Namespace InkAnnotation
Class Program
Private Shared Sub Main(args As String())
'加载PDF文档
Dim pdf As New PdfDocument()
pdf.LoadFromFile("test.pdf")
'获取第一页
Dim pdfPage As PdfPageBase = pdf.Pages(0)
'设置墨迹坐标点位置
Dim inkList As New List(Of Integer())()
Dim intPoints As Integer() = New Integer() {370, 700, 120, 720, 110, 760, _
220, 800, 270, 790, 350, 770, _
350, 670}
inkList.Add(intPoints)
'添加墨迹注释到PDF页面
Dim inkannotation As New PdfInkAnnotation(inkList)
inkannotation.Color = Color.MediumVioletRed
inkannotation.Border.Width = 6
inkannotation.Opacity = 0.5F
inkannotation.Text = "This is an ink annotation. "
inkannotation.Name = "Manager"
pdfPage.AnnotationsWidget.Add(inkannotation)
'保存文档
pdf.SaveToFile("AddInkAnnotation.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
注释效果:
来源:https://www.cnblogs.com/Yesi/archive/2022/02/17/15903542.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现图书检索系统
![](https://img.aspxhome.com/file/2023/1/66681_0s.jpg)
Android SharedPreferences存储用法详解
![](https://img.aspxhome.com/file/2023/8/83528_0s.gif)
Android 消息机制详解及实例代码
![](https://img.aspxhome.com/file/2023/3/85963_0s.png)
关于Mybatis-Plus Update更新策略问题
![](https://img.aspxhome.com/file/2023/6/61276_0s.jpg)
Netty与NIO超详细讲解
![](https://img.aspxhome.com/file/2023/2/65362_0s.png)
spring中使用@Autowired注解无法注入的情况及解决
![](https://img.aspxhome.com/file/2023/9/64159_0s.jpg)
Java设计模式之访问者模式使用场景及代码示例
Spring的异常重试框架Spring Retry简单配置操作
Java异常学习之自定义异常详解
![](https://img.aspxhome.com/file/2023/7/83157_0s.jpg)
SpringBoot过滤器的使用
![](https://img.aspxhome.com/file/2023/3/58313_0s.png)
Java实现Web应用中的定时任务(实例讲解)
一篇文章弄懂JVM类加载机制过程以及原理
![](https://img.aspxhome.com/file/2023/3/88143_0s.png)
Java内存区域管理详解
![](https://img.aspxhome.com/file/2023/3/59173_0s.png)
List调用toString()方法后,去除两头的中括号实例
Java ArrayList扩容问题实例详解
C# 表达式目录树Expression的实现
![](https://img.aspxhome.com/file/2023/0/85670_0s.png)
Spring4如何自定义@Value功能详解
RegexOptions.IgnoreCase正则表达式替换,忽略大小写
Java使用备忘录模式实现过关类游戏功能详解
![](https://img.aspxhome.com/file/2023/2/60782_0s.png)
C# 创建EXCEL图表并保存为图片的实例
![](https://img.aspxhome.com/file/2023/7/81047_0s.png)