C# 在PDF中添加墨迹注释Ink Annotation的步骤详解

作者:E-iceblue 时间:2022-01-31 14:23:29 

PDF中的墨迹注释(Ink Annotation),表现为徒手涂鸦式的形状;该类型的注释,可任意指定形状顶点的位置及个数,通过指定的顶点,程序将连接各点绘制成平滑的曲线。下面,通过C#程序代码介绍如何在PDF中添加该注释。

一、dll引用

步骤1:在Visual Studio中打开“解决方案资源管理器”- 鼠标右键点击“引用”-“管理NuGet包”。

C# 在PDF中添加墨迹注释Ink Annotation的步骤详解

步骤2:选择“浏览”-在搜索框中输入搜索内容,选择搜索结果,点击“安装”。

C# 在PDF中添加墨迹注释Ink Annotation的步骤详解

步骤3:依次点击“OK”-"接受",然后等待程序完成安装。

C# 在PDF中添加墨迹注释Ink Annotation的步骤详解

或者,通过官方渠道,下载包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

注释效果:

C# 在PDF中添加墨迹注释Ink Annotation的步骤详解

来源:https://www.cnblogs.com/Yesi/archive/2022/02/17/15903542.html

标签:C#,PDF,注释
0
投稿

猜你喜欢

  • java实现图书检索系统

    2023-08-18 20:08:50
  • Android SharedPreferences存储用法详解

    2023-08-07 08:25:34
  • Android 消息机制详解及实例代码

    2023-07-28 08:37:49
  • 关于Mybatis-Plus Update更新策略问题

    2022-04-14 19:29:24
  • Netty与NIO超详细讲解

    2022-08-16 00:44:37
  • spring中使用@Autowired注解无法注入的情况及解决

    2023-06-10 21:48:24
  • Java设计模式之访问者模式使用场景及代码示例

    2021-06-27 13:34:25
  • Spring的异常重试框架Spring Retry简单配置操作

    2023-11-25 18:27:35
  • Java异常学习之自定义异常详解

    2023-09-25 00:57:27
  • SpringBoot过滤器的使用

    2023-08-28 21:28:56
  • Java实现Web应用中的定时任务(实例讲解)

    2022-08-12 23:40:02
  • 一篇文章弄懂JVM类加载机制过程以及原理

    2022-01-19 10:32:57
  • Java内存区域管理详解

    2023-11-10 23:44:42
  • List调用toString()方法后,去除两头的中括号实例

    2023-09-28 11:18:56
  • Java ArrayList扩容问题实例详解

    2022-05-08 08:57:57
  • C# 表达式目录树Expression的实现

    2023-04-03 22:57:32
  • Spring4如何自定义@Value功能详解

    2021-12-16 06:31:12
  • RegexOptions.IgnoreCase正则表达式替换,忽略大小写

    2022-05-03 17:25:45
  • Java使用备忘录模式实现过关类游戏功能详解

    2022-11-30 08:52:51
  • C# 创建EXCEL图表并保存为图片的实例

    2023-03-07 07:27:24
  • asp之家 软件编程 m.aspxhome.com