.NET使用C#添加動作到PDF文檔
使用C#語言在.NET框架下向PDF文檔中添加動作,不僅能夠提升文檔的交互性和用戶體驗,還能夠在自動化工作流中發(fā)揮關鍵作用,例如自動跳轉至特定頁面、鏈接外部資源或播放音頻資源等操作。這種能力使得開發(fā)者能夠根據具體需求定制PDF文檔的互動操作,進而提高文檔的實用性。本文將介紹如何在.NET平臺使用C#在PDF文檔中添加動作。
本文所使用的方法需要用到免費Free Spire.PDF for .NET,可通過NuGet安裝:PM> Install-Package Spire.PDF。
用C#在PDF中添加動作的一般步驟
利用C#以及該庫可以向PDF文檔中嵌入多種互動組件動作,如瀏覽控制按鈕、外部文件和網頁連接以及聲音播放功能,以此來提升用戶的閱讀體驗。下面簡要介紹實現PDF內的動作添加的主要步驟:
創(chuàng)建PdfDocument類的實例。
通過PdfDocument.LoadFromFile()方法加載 PDF 文檔。
使用PdfDocument.Pages[]屬性獲取頁面。
創(chuàng)建表示動作的類的實例,并設置其屬性。
將動作添加到PDF文檔:
- 可以使用動作在頁面的矩形區(qū)域內創(chuàng)建PdfActionAnnotation類的實例,并為動作添加提示文字(可選)。然后使用PdfPageBase.Annotations.Add()方法將動作注釋添加到頁面上,從而創(chuàng)建可點擊觸發(fā)的動作。
- 也可以通過PdfDocument.AfterOpenAction、PdfDocument.BeforeCloseAction等屬性直接將動作設置為在進行其他特定操作時執(zhí)行的動作。
使用PdfDocument.SaveToFile()方法保存生成的文檔。
釋放資源。
在PDF中創(chuàng)建文檔內跳轉動作
文檔內跳轉動作的創(chuàng)建通過PdfGoToAction類實現。代碼示例:
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)建網頁鏈接打開動作
網頁鏈接打開動作的創(chuàng)建通過PdfUriAction類實現。代碼示例:
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);
// 在矩形內繪制文本
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 14f, FontStyle.Bold), true);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("點擊跳轉示例網頁", font, PdfBrushes.LightSkyBlue, rect);
// 創(chuàng)建 PdfUriAction 實例并設置其屬性
PdfUriAction action = new PdfUriAction();
action.Uri = "https://www.example.com/";
// 使用網頁鏈接動作和矩形創(chuàng)建 PdfActionAnnotation 實例
PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);
// 將動作注釋添加到第一頁
page.Annotations.Add(actionAnnotation);
// 保存文檔
pdf.SaveToFile("output/PDF網頁鏈接打開動作.pdf");
pdf.Close();
}
}
}
結果

在PDF中創(chuàng)建音頻播放動作
音頻播放動作的創(chuàng)建通過PdfSoundAction類實現。代碼示例:
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");
// 設置聲音參數
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類實現。代碼示例:
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);
// 在矩形內繪制文本
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類實現。代碼示例:
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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#模板方法模式(Template Method Pattern)實例教程
這篇文章主要介紹了C#模板方法模式(Template Method Pattern),以實例形式講述了C#抽象類模板方法的用法,具有很高的實用價值,需要的朋友可以參考下2014-09-09

