尽管常见的PDF阅读器(如Adobe Reader、Foxit Reader)都有页面跳转按钮,有时候我们希望能直接点击文档某处,跳转至相应页面。本文将介绍如何使用Spire.PDF实现页面跳转。
跳转至特殊页(上一页、下一页、首页、最后一页)
C#
//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("C# Tutorials.pdf");
//允许添加Form
doc.AllowCreateForm = true;
//获取最后一页
PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1];
//在页面指定位置添加按钮
PdfButtonField button = new PdfButtonField(lastPage, "返回首页");
button.Bounds = new RectangleF(lastPage.ActualSize.Width - 110, lastPage.ActualSize.Height - 250, 60, 20);
//设置按钮边框颜色
button.BorderStyle = PdfBorderStyle.Solid;
button.BorderColor = new PdfRGBColor(Color.LightPink);
//设置按钮背景色
button.BackColor = Color.Green;
//设置按钮提示语
button.ToolTip = "点击此处返回首页";
//设置按钮文字字体和颜色
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("宋体", 10f), 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);
VB.NET
'加载PDF文档
Dim doc As New PdfDocument()
doc.LoadFromFile("C# Tutorials.pdf")
'允许添加Form
doc.AllowCreateForm = True
'获取最后一页
Dim lastPage As PdfPageBase = doc.Pages(doc.Pages.Count - 1)
'在页面指定位置添加按钮
Dim button As New PdfButtonField(lastPage, "返回首页")
button.Bounds = New RectangleF(lastPage.ActualSize.Width - 110, lastPage.ActualSize.Height - 250, 60, 20)
'设置按钮边框颜色
button.BorderStyle = PdfBorderStyle.Solid
button.BorderColor = New PdfRGBColor(Color.LightPink)
'设置按钮背景色
button.BackColor = Color.Green
'设置按钮提示语
button.ToolTip = "点击此处返回首页"
'设置按钮文字字体和颜色
Dim trueTypeFont As New PdfTrueTypeFont(New Font("宋体", 10F), True)
button.Font = trueTypeFont
button.ForeColor = Color.Black
'创建PdfNamedAction实例,在传入的参数中选择上一页、下一页、首页或最后一页
Dim namedAction As New PdfNamedAction(PdfActionDestination.FirstPage)
'应用动作
button.Actions.MouseDown = namedAction
'添加按钮到文档
doc.Form.Fields.Add(button)
'保存PDF文档
doc.SaveToFile("result.pdf", FileFormat.PDF)
跳转至指定页
C#
//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("C# Tutorials.pdf");
//允许添加Form
doc.AllowCreateForm = true;
//获取最后一页
PdfPageBase lastPage = doc.Pages[doc.Pages.Count - 1];
//在页面指定位置添加按钮
PdfButtonField button = new PdfButtonField(lastPage, "返回至第三页");
button.Bounds = new RectangleF(lastPage.ActualSize.Width - 150, lastPage.ActualSize.Height - 250, 100, 20);
//设置按钮边框颜色
button.BorderStyle = PdfBorderStyle.Solid;
button.BorderColor = new PdfRGBColor(Color.LightPink);
//设置按钮背景色
button.BackColor = Color.LightBlue;
//设置按钮提示语
button.ToolTip = "点击此处返回至第三页";
//设置按钮文字字体和颜色
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font("宋体", 10f), true);
button.Font = trueTypeFont;
button.ForeColor = Color.Black;
//实例化PdfDestination对象,传入指定页码
PdfDestination destination = new PdfDestination(doc.Pages[2]);
//创建go to动作
PdfGoToAction goToAction = new PdfGoToAction(destination);
//应用动作
button.Actions.MouseDown = goToAction;
//添加按钮到文档
doc.Form.Fields.Add(button);
//保存PDF文档
doc.SaveToFile("result.pdf", FileFormat.PDF);
VB.NET
'加载PDF文档
Dim doc As New PdfDocument()
doc.LoadFromFile("C# Tutorials.pdf")
'允许添加Form
doc.AllowCreateForm = True
'获取最后一页
Dim lastPage As PdfPageBase = doc.Pages(doc.Pages.Count - 1)
'在页面指定位置添加按钮
Dim button As New PdfButtonField(lastPage, "返回至第三页")
button.Bounds = New RectangleF(lastPage.ActualSize.Width - 150, lastPage.ActualSize.Height - 250, 100, 20)
'设置按钮边框颜色
button.BorderStyle = PdfBorderStyle.Solid
button.BorderColor = New PdfRGBColor(Color.LightPink)
'设置按钮背景色
button.BackColor = Color.LightBlue
'设置按钮提示语
button.ToolTip = "点击此处返回至第三页"
'设置按钮文字字体和颜色
Dim trueTypeFont As New PdfTrueTypeFont(New Font("宋体", 10F), True)
button.Font = trueTypeFont
button.ForeColor = Color.Black
'实例化PdfDestination对象,传入指定页码
Dim destination As New PdfDestination(doc.Pages(2))
'创建go to动作
Dim goToAction As New PdfGoToAction(destination)
'应用动作
button.Actions.MouseDown = goToAction
'添加按钮到文档
doc.Form.Fields.Add(button)
'保存PDF文档
doc.SaveToFile("result.pdf", FileFormat.PDF)