前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

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

作者头像
很酷的站长
发布2022-11-24 17:44:32
1.2K0
发布2022-11-24 17:44:32
举报

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

一、dll引用

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

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

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

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

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

C#在PDF中添加墨迹注释Ink Annotation的步骤详解
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#

代码语言:javascript
复制
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

代码语言:javascript
复制
Imports Spire.PdfImports Spire.Pdf.AnnotationsImports System.Collections.GenericImports System.DrawingNamespace 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 ClassEnd Namespace

注释效果:

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

到此这篇关于C# 在PDF中添加墨迹注释Ink Annotation的文章就介绍到这了

收藏 | 0点赞 | 0打赏

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档