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