.NET使用C#添加動(dòng)作到PDF文檔
使用C#語(yǔ)言在.NET框架下向PDF文檔中添加動(dòng)作,不僅能夠提升文檔的交互性和用戶體驗(yàn),還能夠在自動(dòng)化工作流中發(fā)揮關(guān)鍵作用,例如自動(dòng)跳轉(zhuǎn)至特定頁(yè)面、鏈接外部資源或播放音頻資源等操作。這種能力使得開發(fā)者能夠根據(jù)具體需求定制PDF文檔的互動(dòng)操作,進(jìn)而提高文檔的實(shí)用性。本文將介紹如何在.NET平臺(tái)使用C#在PDF文檔中添加動(dòng)作。
本文所使用的方法需要用到免費(fèi)Free Spire.PDF for .NET,可通過(guò)NuGet安裝:PM> Install-Package Spire.PDF
。
用C#在PDF中添加動(dòng)作的一般步驟
利用C#以及該庫(kù)可以向PDF文檔中嵌入多種互動(dòng)組件動(dòng)作,如瀏覽控制按鈕、外部文件和網(wǎng)頁(yè)連接以及聲音播放功能,以此來(lái)提升用戶的閱讀體驗(yàn)。下面簡(jiǎn)要介紹實(shí)現(xiàn)PDF內(nèi)的動(dòng)作添加的主要步驟:
創(chuàng)建PdfDocument類的實(shí)例。
通過(guò)PdfDocument.LoadFromFile()方法加載 PDF 文檔。
使用PdfDocument.Pages[]屬性獲取頁(yè)面。
創(chuàng)建表示動(dòng)作的類的實(shí)例,并設(shè)置其屬性。
將動(dòng)作添加到PDF文檔:
- 可以使用動(dòng)作在頁(yè)面的矩形區(qū)域內(nèi)創(chuàng)建PdfActionAnnotation類的實(shí)例,并為動(dòng)作添加提示文字(可選)。然后使用PdfPageBase.Annotations.Add()方法將動(dòng)作注釋添加到頁(yè)面上,從而創(chuàng)建可點(diǎn)擊觸發(fā)的動(dòng)作。
- 也可以通過(guò)PdfDocument.AfterOpenAction、PdfDocument.BeforeCloseAction等屬性直接將動(dòng)作設(shè)置為在進(jìn)行其他特定操作時(shí)執(zhí)行的動(dòng)作。
使用PdfDocument.SaveToFile()方法保存生成的文檔。
釋放資源。
在PDF中創(chuàng)建文檔內(nèi)跳轉(zhuǎn)動(dòng)作
文檔內(nèi)跳轉(zhuǎn)動(dòng)作的創(chuàng)建通過(guò)PdfGoToAction類實(shí)現(xiàn)。代碼示例:
using Spire.Pdf; using Spire.Pdf.Actions; using Spire.Pdf.Annotations; using Spire.Pdf.General; using Spire.Pdf.Graphics; using System.Drawing; namespace AddNavigationButtonPDF { class Program { static void Main(string[] args) { // 創(chuàng)建 PdfDocument 的實(shí)例 PdfDocument pdf = new PdfDocument(); // 加載 PDF 文件 pdf.LoadFromFile("示例.pdf"); // 創(chuàng)建 PdfDestination 實(shí)例并設(shè)置目標(biāo)位置 PdfDestination destination = new PdfDestination(pdf.Pages[1]); destination.Location = new PointF(0, 0); destination.Mode = PdfDestinationMode.Location; destination.Zoom = 0.6f; // 基于目標(biāo)位置創(chuàng)建 PdfGoToAction 實(shí)例 PdfGoToAction action = new PdfGoToAction(destination); // 創(chuàng)建矩形并繪制到第一頁(yè) RectangleF rect = new RectangleF(70, pdf.PageSettings.Size.Height - 120, 140, 20); pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.LightGray, rect); // 在矩形中繪制文本 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 14f, FontStyle.Bold), true); PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center); pdf.Pages[0].Canvas.DrawString("跳轉(zhuǎn)到第2頁(yè)", font, PdfBrushes.Green, rect, stringFormat); // 基于矩形和動(dòng)作創(chuàng)建 PdfActionAnnotation 實(shí)例 PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action); // 將動(dòng)作注釋添加到第一頁(yè) pdf.Pages[0].Annotations.Add(actionAnnotation); // 保存文檔 pdf.SaveToFile("output/PDF導(dǎo)航動(dòng)作.pdf"); pdf.Close(); } } }
結(jié)果
在PDF中創(chuàng)建網(wǎng)頁(yè)鏈接打開動(dòng)作
網(wǎng)頁(yè)鏈接打開動(dòng)作的創(chuàng)建通過(guò)PdfUriAction類實(shí)現(xiàn)。代碼示例:
using Spire.Pdf; using Spire.Pdf.Actions; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace AddSoundActionPDF { class Program { static void Main(string[] args) { // 創(chuàng)建 PdfDocument 的實(shí)例 PdfDocument pdf = new PdfDocument(); // 加載 PDF 文件 pdf.LoadFromFile("示例.pdf"); // 獲取第一頁(yè) PdfPageBase page = pdf.Pages[0]; // 在頁(yè)面上繪制矩形 RectangleF rect = new RectangleF(30, 30, 120, 20); page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect); // 在矩形內(nèi)繪制文本 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 14f, FontStyle.Bold), true); PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("點(diǎn)擊跳轉(zhuǎn)示例網(wǎng)頁(yè)", font, PdfBrushes.LightSkyBlue, rect); // 創(chuàng)建 PdfUriAction 實(shí)例并設(shè)置其屬性 PdfUriAction action = new PdfUriAction(); action.Uri = "https://www.example.com/"; // 使用網(wǎng)頁(yè)鏈接動(dòng)作和矩形創(chuàng)建 PdfActionAnnotation 實(shí)例 PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action); // 將動(dòng)作注釋添加到第一頁(yè) page.Annotations.Add(actionAnnotation); // 保存文檔 pdf.SaveToFile("output/PDF網(wǎng)頁(yè)鏈接打開動(dòng)作.pdf"); pdf.Close(); } } }
結(jié)果
在PDF中創(chuàng)建音頻播放動(dòng)作
音頻播放動(dòng)作的創(chuàng)建通過(guò)PdfSoundAction類實(shí)現(xiàn)。代碼示例:
using Spire.Pdf; using Spire.Pdf.Actions; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using Spire.Pdf.General; using System.Drawing; namespace AddSoundActionPDF { class Program { static void Main(string[] args) { // 創(chuàng)建 PdfDocument 的實(shí)例 PdfDocument pdf = new PdfDocument(); // 加載 PDF 文件 pdf.LoadFromFile("示例.pdf"); // 獲取第一頁(yè) PdfPageBase page = pdf.Pages[0]; // 在頁(yè)面上繪制提示圖像 PdfImage image = PdfImage.FromFile("音頻.png"); page.Canvas.DrawImage(image, new PointF(30, 30)); // 創(chuàng)建 PdfSoundAction 實(shí)例并設(shè)置其屬性 PdfSoundAction action = new PdfSoundAction("背景.wav"); // 設(shè)置聲音參數(shù) action.Sound.Bits = 16; action.Sound.Channels = PdfSoundChannels.Stereo; action.Sound.Encoding = PdfSoundEncoding.Signed; action.Sound.Rate = 44100; // 設(shè)置播放選項(xiàng) action.Volume = 0; action.Repeat = true; action.Mix = true; action.Synchronous = true; // 基于提示圖像的位置創(chuàng)建 PdfActionAnnotation 實(shí)例,用于聲音動(dòng)作 RectangleF rect = new RectangleF(30, 30, image.Width, image.Height); PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action); // 將動(dòng)作注釋添加到第一頁(yè) page.Annotations.Add(actionAnnotation); // 設(shè)置在文檔打開后播放聲音動(dòng)作 pdf.AfterOpenAction = action; // 保存文檔 pdf.SaveToFile("output/PDF音頻播放動(dòng)作.pdf"); pdf.Close(); } } }
結(jié)果
在PDF中創(chuàng)建文件打開動(dòng)作
文件打開動(dòng)作的創(chuàng)建通過(guò)PdfLaunchAction類實(shí)現(xiàn)。代碼示例:
using Spire.Pdf; using Spire.Pdf.Actions; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace AddFileLaunchActionPDF { class Program { static void Main(string[] args) { // 創(chuàng)建 PdfDocument 的實(shí)例 PdfDocument pdf = new PdfDocument(); // 加載 PDF 文件 pdf.LoadFromFile("示例.pdf"); // 獲取第一頁(yè) PdfPageBase page = pdf.Pages[0]; // 在頁(yè)面上繪制矩形 RectangleF rect = new RectangleF(50, 50, 180, 20); page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect); // 在矩形內(nèi)繪制文本 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 14f, FontStyle.Bold), true); PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center); pdf.Pages[0].Canvas.DrawString("點(diǎn)擊打開示例2", font, PdfBrushes.Green, rect, stringFormat); // 創(chuàng)建 PdfLaunchAction 實(shí)例 PdfLaunchAction action = new PdfLaunchAction("D:/示例2.pdf", PdfFilePathType.Absolute); // 設(shè)置啟動(dòng)模式為在新窗口中打開 action.IsNewWindow = true; // 基于矩形和啟動(dòng)動(dòng)作創(chuàng)建 PdfActionAnnotation 實(shí)例 PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action); // 將動(dòng)作注釋添加到第一頁(yè) page.Annotations.Add(actionAnnotation); // 保存文檔 pdf.SaveToFile("output/PDF文件打開動(dòng)作.pdf"); pdf.Close(); } } }
結(jié)果
在PDF中創(chuàng)建JavaScript動(dòng)作
JavaScript動(dòng)作的創(chuàng)建通過(guò)PdfJavaScriptAction類實(shí)現(xiàn)。代碼示例:
using Spire.Pdf; using Spire.Pdf.Actions; namespace AddJavaScriptActionPDF { class Program { static void Main(string[] args) { // 創(chuàng)建 PdfDocument 的實(shí)例 PdfDocument pdf = new PdfDocument(); // 加載 PDF 文件 pdf.LoadFromFile("示例.pdf"); // 定義JavaScript代碼 string jsCode = "app.alert({" + " cMsg: '歡迎閱讀《水星:太陽(yáng)系中最小的行星之一,卻擁有無(wú)盡的科學(xué)奧秘》。\\n\\n本文將詳細(xì)探討水星的各個(gè)方面,包括概述、形成和歷史、表面特征、氣候和環(huán)境,以及未來(lái)的探索。', " + " nIcon: 3, " + " cTitle: '文檔介紹'" + "});"; // 使用代碼創(chuàng)建 PdfJavaScriptAction 實(shí)例 PdfJavaScriptAction action = new PdfJavaScriptAction(jsCode); // 將動(dòng)作設(shè)置為PDF文檔打開時(shí)執(zhí)行 pdf.AfterOpenAction = action; // 保存文檔 pdf.SaveToFile("output/PDF JavaScript動(dòng)作.pdf"); pdf.Close(); } } }
結(jié)果
到此這篇關(guān)于.NET使用C#添加動(dòng)作到PDF文檔的文章就介紹到這了,更多相關(guān)C#添加動(dòng)作到PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)餐飲管理系統(tǒng)完整版
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)餐飲管理系統(tǒng)的完整版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01C#獲取真實(shí)IP地址(IP轉(zhuǎn)為長(zhǎng)整形、判斷是否內(nèi)網(wǎng)IP的方法)
這篇文章主要介紹了C#獲取真實(shí)IP地址的實(shí)現(xiàn)代碼,包含把IP轉(zhuǎn)為長(zhǎng)整形、判斷是否是私網(wǎng)、內(nèi)網(wǎng)IP的方法,需要的朋友可以參考下2014-08-08C# DataTable中查詢指定字段名稱的數(shù)據(jù)
這篇文章主要介紹了C# DataTable中查詢指定字段名稱的數(shù)據(jù),本文直接給出實(shí)例代碼,簡(jiǎn)單易懂,需要的朋友可以參考下2015-06-06利用Aspose.Word控件實(shí)現(xiàn)Word文檔的操作
偶然一次機(jī)會(huì),一個(gè)項(xiàng)目的報(bào)表功能指定需要導(dǎo)出為Word文檔,因此尋找了很多篇文章,不過(guò)多數(shù)介紹的比較簡(jiǎn)單一點(diǎn),于是也參考了官方的幫助介紹,終于滿足了客戶的需求。下面我由淺入深來(lái)介紹這個(gè)控件在實(shí)際業(yè)務(wù)中的使用過(guò)程吧2013-05-05