提升 PDF 文件的交互性是文档管理和用户体验优化的重要组成部分。通过 .NET 框架中的 C#,开发人员可以在 PDF 文档中创建各种操作,例如添加文件链接、网页链接和音频,实现页面跳转、启动外部应用程序或播放背景音乐等功能,从而让 PDF 文档具备更丰富的交互功能。
本文将介绍如何使用 C# 在 PDF 文档中创建各种操作。
开始之前,需要将相关 DLL 文件添加为 .NET 项目的引用。可以下载相应的软件包,也可以通过 NuGet 包管理器进行安装。
PM> Install-Package Spire.PDF在 PDF 文档中添加操作,可以实现页面导航、文件链接、网页链接和声音播放等交互功能。使用 C# 开发时,可以通过相应的类创建不同类型的操作。以下是常见操作类及其功能说明:
类名 | 功能说明 |
|---|---|
| 跳转到当前文档中的指定位置。 |
| 打开指定文件。 |
| 播放声音。 |
| 执行 PDF 文档中的 JavaScript 代码。 |
| 处理统一资源标识符(URI),例如访问指定的网页地址。 |
在 PDF 文档中添加操作,通常有以下两种方式。
这种方式是创建一个操作,并将其关联到页面上的注释。用户单击相应的注释区域时,即可触发该操作。
基本步骤如下:
PdfDocument 类的实例,并使用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。PdfDocument.Pages[] 属性获取目标页面。PdfLinkAnnotation 类的实例,在页面的指定矩形区域内添加链接注释,并关联相应操作。PdfAnnotationCollection.Create(page) 方法获取注释集合,并通过 Add() 方法添加注释。PdfDocument.SaveToFile() 方法保存修改后的 PDF 文档。除了通过页面注释触发操作,还可以将操作关联到文档级事件,例如打开、关闭、保存或打印文档。当指定事件发生时,相应操作会自动触发。
基本步骤如下:
PdfDocument 类的实例,并使用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。PdfDocument.AfterOpenActionPdfDocument.AfterSaveActionPdfDocument.AfterPrintActionPdfDocument.BeforeCloseActionPdfDocument.BeforeSaveActionPdfDocument.BeforePrintActionPdfDocument.SaveToFile() 方法保存修改后的 PDF 文档。可以使用 PdfGoToAction 类创建页面导航操作,使用户能够跳转到文档中的指定位置。具体来说,可以先创建 PdfXYZExplicitDestination 对象,再将其作为参数传递给 PdfGoToAction 实例。
以下代码示例演示了如何在 PDF 文档中添加页面导航操作:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Interactive.Annotations;
using Spire.Pdf.Destinations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddNavigationButtonPDF
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 实例
PdfDocument pdf = new PdfDocument();
// 加载 PDF 文件
pdf.LoadFromFile("Sample.pdf");
// 创建 PdfXYZExplicitDestination 实例并设置目标位置
PdfXYZExplicitDestination destination = new PdfXYZExplicitDestination(pdf.Pages[1], 0f, 0f, 0.8f);
// 根据目标位置创建 PdfGoToAction 实例
PdfGoToAction action = new PdfGoToAction(destination);
// 创建矩形区域并将其绘制在第一页上
RectangleF rect = new RectangleF(70, pdf.PageSettings.Size.Height - 120, 140, 20);
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
// 在矩形区域中绘制提示文字
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
pdf.Pages[0].Canvas.DrawString("To Page 2", font, PdfBrushes.Green, rect, stringFormat);
// 根据矩形区域和操作创建 PdfLinkAnnotation 实例
PdfLinkAnnotation actionAnnotation = new PdfLinkAnnotation(pdf.Pages[0], rect, action);
// 将操作注释添加到第一页
PdfAnnotationCollection.Create(pdf.Pages[0]).Add(actionAnnotation);
// 保存文档
pdf.SaveToFile("output/NavigationButton.pdf");
pdf.Close();
}
}
}PdfLaunchAction 类用于在 PDF 文档中创建文件打开操作。用户单击 PDF 页面中嵌入的按钮后,即可打开指定文件。
开发人员可以设置文件路径(绝对路径或相对路径),并指定是否在新窗口中打开文件。
以下代码示例演示了如何在 PDF 文档中添加文件打开操作:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Interactive.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddFileLaunchActionPDF
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 实例
PdfDocument pdf = new PdfDocument();
// 加载 PDF 文件
pdf.LoadFromFile("Sample.pdf");
// 获取第一页
PdfPageBase page = pdf.Pages[0];
// 在页面上绘制一个矩形
RectangleF rect = new RectangleF(50, 50, 180, 20);
page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
// 在矩形中绘制提示文字
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
pdf.Pages[0].Canvas.DrawString("单击以打开 Sample2", font, PdfBrushes.Green, rect, stringFormat);
// 创建 PdfLaunchAction 实例
PdfLaunchAction action = new PdfLaunchAction("C:/Sample2.pdf", PdfFilePathType.Absolute);
// 设置启动模式为在新窗口中打开
action.NewWindow = true;
// 根据矩形区域和文件打开操作创建 PdfLinkAnnotation 实例
PdfLinkAnnotation actionAnnotation = new PdfLinkAnnotation(page, rect, action);
// 将操作注释添加到第一页
PdfAnnotationCollection.Create(page).Add(actionAnnotation);
// 保存文档
pdf.SaveToFile("output/LaunchAction.pdf");
pdf.Close();
}
}
}通过 PdfSoundAction 类,可以将音频添加为 PDF 文档中的操作,使其在特定条件触发时播放,例如打开文件或单击按钮。
以下代码示例演示了如何在 PDF 文档中创建声音播放操作:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Interactive.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddSoundActionPDF
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 实例
PdfDocument pdf = new PdfDocument();
// 加载 PDF 文件
pdf.LoadFromFile("Sample.pdf");
// 获取第一页
PdfPageBase page = pdf.Pages[0];
// 在页面上绘制提示图片
PdfImage image = PdfImage.FromFile("Sound.png");
page.Canvas.DrawImage(image, new PointF(30, 30));
// 创建 PdfSoundAction 实例并设置相关属性
PdfSoundAction action = new PdfSoundAction("Wave.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;
// 在提示图片所在的位置创建包含声音播放操作的 PdfLinkAnnotation 实例
RectangleF rect = new RectangleF(30, 30, image.Width, image.Height);
PdfLinkAnnotation actionAnnotation = new PdfLinkAnnotation(page, rect, action);
// 将操作注释添加到第一页
PdfAnnotationCollection.Create(page).Add(actionAnnotation);
// 设置文档打开后自动播放声音
pdf.AfterOpenAction = action;
// 保存文档
pdf.SaveToFile("output/SoundAction.pdf");
pdf.Close();
}
}
}通过 PdfURIAction 类,可以在 PDF 文档中添加网页链接。用户执行特定操作(例如单击按钮)时,即可打开指定的网页。
以下代码示例演示了如何在 PDF 文档中创建网页链接操作:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Interactive.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddUriActionPDF
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 实例
PdfDocument pdf = new PdfDocument();
// 加载 PDF 文件
pdf.LoadFromFile("Sample.pdf");
// 获取第一页
PdfPageBase page = pdf.Pages[0];
// 在页面上绘制一个矩形
RectangleF rect = new RectangleF(30, 30, 120, 20);
page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
// 在矩形中绘制提示文字
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("前往 Google 搜索", font, PdfBrushes.LightSkyBlue, rect);
// 创建 PdfURIAction 实例并设置相关属性
PdfURIAction action = new PdfURIAction("https://www.google.com/");
// 根据网页链接操作和矩形区域创建 PdfLinkAnnotation 实例
PdfLinkAnnotation actionAnnotation = new PdfLinkAnnotation(page, rect, action);
// 将操作注释添加到第一页
PdfAnnotationCollection.Create(page).Add(actionAnnotation);
// 保存文档
pdf.SaveToFile("output/WebLinkAction.pdf");
pdf.Close();
}
}
}PdfJavaScriptAction 类用于在 PDF 文档中添加 JavaScript 操作,可实现表单验证、数据计算和自定义用户界面等交互功能。
以下代码示例演示了如何使用 C# 在 PDF 文档中添加一个简单的 JavaScript 操作:
using Spire.Pdf;
using Spire.Pdf.Actions;
namespace AddJavaScriptActionPDF
{
class Program
{
static void Main(string[] args)
{
// 创建 PdfDocument 实例
PdfDocument pdf = new PdfDocument();
// 加载 PDF 文件
pdf.LoadFromFile("Sample.pdf");
// 定义 JavaScript 代码
var jsCode =
"app.alert({" +
" cMsg: '欢迎阅读关于 LCD 屏幕发展历程与优势的文章!\\n\\n本文将介绍 LCD 技术的历史及其优势,包括这项技术对各种设备视觉体验的影响。', " +
" nIcon: 3, " +
" cTitle: '文档简介'" +
"});";
// 使用 JavaScript 代码创建 PdfJavaScriptAction 实例
PdfJavaScriptAction action = new PdfJavaScriptAction(jsCode);
// 设置 PDF 文档打开后执行的操作
pdf.AfterOpenAction = action;
// 保存文档
pdf.SaveToFile("output/PDFJavaScriptAction.pdf");
pdf.Close();
}
}
}通过 C#,开发人员可以为 PDF 文档添加多种交互操作,例如页面跳转、文件打开、声音播放、网页链接和 JavaScript 执行等。这些操作能够根据用户的点击行为或文档打开等事件触发,从而增强 PDF 文档的交互性。
本文介绍了如何使用不同的操作类创建相应功能,并通过代码示例展示了具体实现方式。在实际应用中,可以根据文档的使用场景选择合适的操作类型,同时注意文件路径、触发条件及 JavaScript 执行环境等因素,以确保功能正常运行。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。