C#?在PDF中添加墨跡注釋Ink?Annotation的步驟詳解
PDF中的墨跡注釋(Ink Annotation),表現(xiàn)為徒手涂鴉式的形狀;該類型的注釋,可任意指定形狀頂點的位置及個數(shù),通過指定的頂點,程序?qū)⑦B接各點繪制成平滑的曲線。下面,通過C#程序代碼介紹如何在PDF中添加該注釋。
一、dll引用
步驟1:在Visual Studio中打開“解決方案資源管理器”- 鼠標右鍵點擊“引用”-“管理NuGet包”。
步驟2:選擇“瀏覽”-在搜索框中輸入搜索內(nèi)容,選擇搜索結(jié)果,點擊“安裝”。
步驟3:依次點擊“OK”-"接受",然后等待程序完成安裝。
或者,通過官方渠道,下載包Spire.PDF for .NET到本地。解壓后,將BIN文件夾下的Spire.Pdf.dll文件引用至VS程序。
二、代碼示例
添加注釋時,除了自定義各個點的位置及數(shù)量,也可以設置墨跡顏色、線條寬度、透明度、注釋的內(nèi)容、名稱等。下面是代碼實現(xiàn)的步驟:
- 創(chuàng)建PdfDocument類的對象,并通過PdfDocument.LoadFromFile(String fileName)方法加載PDF文檔。
- 通過PdfDocument.Pages[int Index]屬性獲取PDF指定頁面。
- 創(chuàng)建類型為int的對象集合,集合元素為各墨跡頂點。
- 創(chuàng)建PdfInkAnnotation類的實例。并通過該類提供的屬性設置墨跡顏色、寬度、注釋內(nèi)容等格式。
- 調(diào)用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的文章就介紹到這了,更多相關C# PDF添加墨跡注釋內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C# 9 中新加入的關鍵詞 init,record,with
這篇文章主要介紹了C# 9 中新加入的關鍵詞 init,record,with的相關資料,幫助大家更好的理解和學習c# 9,感興趣的朋友可以了解下2020-08-08