基础网站建设公司wordpress网站打开很慢
使用C#语言在.NET框架下向PDF文档中添加动作,不仅能够提升文档的交互性和用户体验,还能够在自动化工作流中发挥关键作用,例如自动跳转至特定页面、链接外部资源或播放音频资源等操作。这种能力使得开发者能够根据具体需求定制PDF文档的互动操作,进而提高文档的实用性。本文将介绍如何在.NET平台使用C#在PDF文档中添加动作。
文章目录
- 用C#在PDF中添加动作的一般步骤
- 在PDF中创建文档内跳转动作
- 在PDF中创建网页链接打开动作
- 在PDF中创建音频播放动作
- 在PDF中创建文件打开动作
- 在PDF中创建JavaScript动作
本文所使用的方法需要用到免费Free Spire.PDF for .NET,可通过NuGet安装:PM> Install-Package Spire.PDF
。
用C#在PDF中添加动作的一般步骤
利用C#以及该库可以向PDF文档中嵌入多种互动组件动作,如浏览控制按钮、外部文件和网页连接以及声音播放功能,以此来提升用户的阅读体验。下面简要介绍实现PDF内的动作添加的主要步骤:
- 创建
PdfDocument
类的实例。 - 通过
PdfDocument.LoadFromFile()
方法加载 PDF 文档。 - 使用
PdfDocument.Pages[]
属性获取页面。 - 创建表示动作的类的实例,并设置其属性。
- 将动作添加到PDF文档:
- 可以使用动作在页面的矩形区域内创建
PdfActionAnnotation
类的实例,并为动作添加提示文字(可选)。然后使用PdfPageBase.Annotations.Add()
方法将动作注释添加到页面上,从而创建可点击触发的动作。 - 也可以通过
PdfDocument.AfterOpenAction
、PdfDocument.BeforeCloseAction
等属性直接将动作设置为在进行其他特定操作时执行的动作。
- 可以使用动作在页面的矩形区域内创建
- 使用
PdfDocument.SaveToFile()
方法保存生成的文档。 - 释放资源。
在PDF中创建文档内跳转动作
文档内跳转动作的创建通过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){// 创建 PdfDocument 的实例PdfDocument pdf = new PdfDocument();// 加载 PDF 文件pdf.LoadFromFile("示例.pdf");// 创建 PdfDestination 实例并设置目标位置PdfDestination destination = new PdfDestination(pdf.Pages[1]);destination.Location = new PointF(0, 0);destination.Mode = PdfDestinationMode.Location;destination.Zoom = 0.6f;// 基于目标位置创建 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);// 在矩形中绘制文本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);// 基于矩形和动作创建 PdfActionAnnotation 实例PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);// 将动作注释添加到第一页pdf.Pages[0].Annotations.Add(actionAnnotation);// 保存文档pdf.SaveToFile("output/PDF导航动作.pdf");pdf.Close();}}
}
结果
在PDF中创建网页链接打开动作
网页链接打开动作的创建通过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){// 创建 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);// 创建 PdfUriAction 实例并设置其属性PdfUriAction action = new PdfUriAction();action.Uri = "https://www.example.com/";// 使用网页链接动作和矩形创建 PdfActionAnnotation 实例PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);// 将动作注释添加到第一页page.Annotations.Add(actionAnnotation);// 保存文档pdf.SaveToFile("output/PDF网页链接打开动作.pdf");pdf.Close();}}
}
结果
在PDF中创建音频播放动作
音频播放动作的创建通过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){// 创建 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));// 创建 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;// 基于提示图像的位置创建 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中创建文件打开动作
文件打开动作的创建通过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){// 创建 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);// 创建 PdfLaunchAction 实例PdfLaunchAction action = new PdfLaunchAction("D:/示例2.pdf", PdfFilePathType.Absolute);// 设置启动模式为在新窗口中打开action.IsNewWindow = true;// 基于矩形和启动动作创建 PdfActionAnnotation 实例PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);// 将动作注释添加到第一页page.Annotations.Add(actionAnnotation);// 保存文档pdf.SaveToFile("output/PDF文件打开动作.pdf");pdf.Close();}}
}
结果
在PDF中创建JavaScript动作
JavaScript动作的创建通过PdfJavaScriptAction
类实现。代码示例:
using Spire.Pdf;
using Spire.Pdf.Actions;namespace AddJavaScriptActionPDF
{class Program{static void Main(string[] args){// 创建 PdfDocument 的实例PdfDocument pdf = new PdfDocument();// 加载 PDF 文件pdf.LoadFromFile("示例.pdf");// 定义JavaScript代码string jsCode ="app.alert({" +" cMsg: '欢迎阅读《水星:太阳系中最小的行星之一,却拥有无尽的科学奥秘》。\\n\\n本文将详细探讨水星的各个方面,包括概述、形成和历史、表面特征、气候和环境,以及未来的探索。', " +" nIcon: 3, " +" cTitle: '文档介绍'" +"});";// 使用代码创建 PdfJavaScriptAction 实例PdfJavaScriptAction action = new PdfJavaScriptAction(jsCode);// 将动作设置为PDF文档打开时执行pdf.AfterOpenAction = action;// 保存文档pdf.SaveToFile("output/PDF JavaScript动作.pdf");pdf.Close();}}
}
结果
本文介绍如何在.NET平台使用C#代码实现在PDF中添加动作,提供步骤介绍及代码示例。