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