C#在PDF中怎么添加墨迹注释
这篇文章主要介绍“C#在PDF中怎么添加墨迹注释”,在日常操作中,相信很多人在C#在PDF中怎么添加墨迹注释问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#在PDF中怎么添加墨迹注释”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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#
usingSpire.Pdf;usingSpire.Pdf.Annotations;usingSystem.Collections.Generic;usingSystem.Drawing;namespaceInkAnnotation{classProgram{staticvoidMain(string[]args){//加载PDF文档PdfDocumentpdf=newPdfDocument();pdf.LoadFromFile("test.pdf");//获取第一页PdfPageBasepdfPage=pdf.Pages[0];//设置墨迹坐标点位置List<int[]>inkList=newList<int[]>();int[]intPoints=newint[]{370,700,120,720,110,760,220,800,270,790,350,770,350,670};inkList.Add(intPoints);//添加墨迹注释到PDF页面PdfInkAnnotationinkannotation=newPdfInkAnnotation(inkList);inkannotation.Color=Color.MediumVioletRed;inkannotation.Border.Width=6;inkannotation.Opacity=0.5f;inkannotation.Text="Thisisaninkannotation.";inkannotation.Name="Manager";pdfPage.AnnotationsWidget.Add(inkannotation);//保存文档Pdf.SaveToFile("AddInkAnnotation.pdf",FileFormat.PDF);System.Diagnostics.Process.Start("AddInkAnnotation.pdf");}}}
vb.net
ImportsSpire.PdfImportsSpire.Pdf.AnnotationsImportsSystem.Collections.GenericImportsSystem.DrawingNamespaceInkAnnotationClassProgramPrivateSharedSubMain(argsAsString())'加载PDF文档DimpdfAsNewPdfDocument()pdf.LoadFromFile("test.pdf")'获取第一页DimpdfPageAsPdfPageBase=pdf.Pages(0)'设置墨迹坐标点位置DiminkListAsNewList(OfInteger())()DimintPointsAsInteger()=NewInteger(){370,700,120,720,110,760,_220,800,270,790,350,770,_350,670}inkList.Add(intPoints)'添加墨迹注释到PDF页面DiminkannotationAsNewPdfInkAnnotation(inkList)inkannotation.Color=Color.MediumVioletRedinkannotation.Border.Width=6inkannotation.Opacity=0.5Finkannotation.Text="Thisisaninkannotation."inkannotation.Name="Manager"pdfPage.AnnotationsWidget.Add(inkannotation)'保存文档pdf.SaveToFile("AddInkAnnotation.pdf",FileFormat.PDF)EndSubEndClassEndNamespace
注释效果:
到此,关于“C#在PDF中怎么添加墨迹注释”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。