.NET使用C#添加動(dòng)作到PDF文檔
使用C#語言在.NET框架下向PDF文檔中添加動(dòng)作,不僅能夠提升文檔的交互性和用戶體驗(yàn),還能夠在自動(dòng)化工作流中發(fā)揮關(guān)鍵作用,例如自動(dòng)跳轉(zhuǎn)至特定頁面、鏈接外部資源或播放音頻資源等操作。這種能力使得開發(fā)者能夠根據(jù)具體需求定制PDF文檔的互動(dòng)操作,進(jìn)而提高文檔的實(shí)用性。本文將介紹如何在.NET平臺使用C#在PDF文檔中添加動(dòng)作。
本文所使用的方法需要用到免費(fèi)Free Spire.PDF for .NET,可通過NuGet安裝:PM> Install-Package Spire.PDF。
用C#在PDF中添加動(dòng)作的一般步驟
利用C#以及該庫可以向PDF文檔中嵌入多種互動(dòng)組件動(dòng)作,如瀏覽控制按鈕、外部文件和網(wǎng)頁連接以及聲音播放功能,以此來提升用戶的閱讀體驗(yàn)。下面簡要介紹實(shí)現(xiàn)PDF內(nèi)的動(dòng)作添加的主要步驟:
創(chuàng)建PdfDocument類的實(shí)例。
通過PdfDocument.LoadFromFile()方法加載 PDF 文檔。
使用PdfDocument.Pages[]屬性獲取頁面。
創(chuàng)建表示動(dòng)作的類的實(shí)例,并設(shè)置其屬性。
將動(dòng)作添加到PDF文檔:
- 可以使用動(dòng)作在頁面的矩形區(qū)域內(nèi)創(chuàng)建PdfActionAnnotation類的實(shí)例,并為動(dòng)作添加提示文字(可選)。然后使用PdfPageBase.Annotations.Add()方法將動(dòng)作注釋添加到頁面上,從而創(chuàng)建可點(diǎn)擊觸發(fā)的動(dòng)作。
- 也可以通過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)建通過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)建矩形并繪制到第一頁
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頁", font, PdfBrushes.Green, rect, stringFormat);
// 基于矩形和動(dòng)作創(chuàng)建 PdfActionAnnotation 實(shí)例
PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);
// 將動(dòng)作注釋添加到第一頁
pdf.Pages[0].Annotations.Add(actionAnnotation);
// 保存文檔
pdf.SaveToFile("output/PDF導(dǎo)航動(dòng)作.pdf");
pdf.Close();
}
}
}
結(jié)果

在PDF中創(chuàng)建網(wǎng)頁鏈接打開動(dòng)作
網(wǎng)頁鏈接打開動(dòng)作的創(chuàng)建通過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");
// 獲取第一頁
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("點(diǎn)擊跳轉(zhuǎn)示例網(wǎng)頁", font, PdfBrushes.LightSkyBlue, rect);
// 創(chuàng)建 PdfUriAction 實(shí)例并設(shè)置其屬性
PdfUriAction action = new PdfUriAction();
action.Uri = "https://www.example.com/";
// 使用網(wǎng)頁鏈接動(dòng)作和矩形創(chuàng)建 PdfActionAnnotation 實(shí)例
PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);
// 將動(dòng)作注釋添加到第一頁
page.Annotations.Add(actionAnnotation);
// 保存文檔
pdf.SaveToFile("output/PDF網(wǎng)頁鏈接打開動(dòng)作.pdf");
pdf.Close();
}
}
}
結(jié)果

在PDF中創(chuàng)建音頻播放動(dòng)作
音頻播放動(dòng)作的創(chuàng)建通過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");
// 獲取第一頁
PdfPageBase page = pdf.Pages[0];
// 在頁面上繪制提示圖像
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)作注釋添加到第一頁
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)建通過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");
// 獲取第一頁
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("點(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)作注釋添加到第一頁
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)建通過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: '歡迎閱讀《水星:太陽系中最小的行星之一,卻擁有無盡的科學(xué)奧秘》。\\n\\n本文將詳細(xì)探討水星的各個(gè)方面,包括概述、形成和歷史、表面特征、氣候和環(huán)境,以及未來的探索。', " +
" 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WinForm實(shí)現(xiàn)鼠標(biāo)拖動(dòng)控件跟隨效果
這篇文章主要為大家詳細(xì)介紹了WinForm實(shí)現(xiàn)鼠標(biāo)拖動(dòng)控件跟隨效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
C#模板方法模式(Template Method Pattern)實(shí)例教程
這篇文章主要介紹了C#模板方法模式(Template Method Pattern),以實(shí)例形式講述了C#抽象類模板方法的用法,具有很高的實(shí)用價(jià)值,需要的朋友可以參考下2014-09-09

