
C#中实现PDF页面切换按钮的Page操作方法
5星
- 浏览量: 0
- 大小:None
- 文件类型:PDF
简介:
本文介绍了在C#编程环境中如何通过使用特定库来创建能够实现PDF文件内页切换功能的按钮。通过这种方法,读者可以学会运用Page类中的相关方法对PDF文档进行有效的页面导航控制,为用户提供更加便捷的操作体验。
本段落主要介绍了如何在 C# 中使用 Spire.PDF for .NET 库来实现 PDF 文档页面设置页面切换按钮的方法。该方法可以将按钮添加到 PDF 文档中,并实现按钮的跳转到指定页面、首页、下一页、上一页或最后一页的功能。
标题:C# PDF Page操作设置页面切换按钮的方法
描述:主要介绍了 C# PDF Page 操作设置页面切换按钮的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下。
标签:C# PDF Page 操作 c# 页面切换按钮
在以下示例中,将介绍在 PDF 文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上页或者最后一页,另一种是设置按钮跳转到指定页面。两种方法适应不同的程序设计需要,可自行选择合适的添加方法。
这里的代码示例需要使用类库 Spire.PDF for .NET,版本 4.0。在使用该类库时,在项目程序中引用 Spire.Pdf.dll 即可(dll 文件在安装路径下的 Bin 文件中获取)。
以下为操作示例:
1. 跳转至特定页(首页、下一页、上一页、最后一页)
```csharp
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace ButtonToAppointedPage_PDF
{
class Program
{
static void Main(string[] args)
{
// 实例化 PdfDocument 类,加载 PDF 测试文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(sample.pdf);
// 允许添加 Form
doc.AllowCreateForm = true;
// 获取文档最后一页
PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1];
// 在页面指定位置添加指定大小的按钮
PdfButtonField button = new PdfButtonField(lastPage, Click To Back);
button.Bounds = new RectangleF(lastPage.ActualSize.Width - 150, lastPage.ActualSize.Height - 400, 60, 20);
// 设置按钮边框颜色
button.BorderStyle = PdfBorderStyle.Solid;
button.BorderColor = new PdfRGBColor(Color.White);
// 设置按钮背景色
button.BackColor = Color.Azure;
// 设置按钮提示语
button.ToolTip = To the first page;
// 设置按钮文字字体和颜色
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font(Avant Garde, 9f), true);
button.Font = trueTypeFont;
button.ForeColor = Color.Black;
// 创建 PdfNamedAction 实例,在传入的参数中选择上一页、下一页、首页或最后一页
PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
// 应用动作
button.Actions.MouseDown = namedAction;
// 添加按钮到文档
doc.Form.Fields.Add(button);
// 保存并打开 PDF 文档
doc.SaveToFile(result.pdf, FileFormat.PDF);
System.Diagnostics.Process.Start(result.pdf);
}
}
}
```
2. 跳转至指定页面
```csharp
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace Buttom2
{
class Program
{
static void Main(string[] args)
{
// 实例化 PdfDocument 类,加载 PDF 文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(sample.pdf);
// ...
}
}
}
```
本段落展现了使用 Spire.PDF for .NET 库来实现 PDF 文档页面设置页面切换按钮的详细步骤,从而帮助开发者快速掌握该技术,并将其应用于实际项目中。
全部评论 (0)


