尽管常见的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(PdfPredefinedAction.FirstPage);
//添加按钮到文档
doc.Form.Fields.Add(button);
//从页面取回按钮的控件注释,把动作挂到它的 OnActivated 上
PdfAnnotationCollection annotations = PdfAnnotationCollection.Create(lastPage);
((PdfWidgetAnnotation)annotations[annotations.Count - 1]).OnActivated = namedAction;
//保存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(PdfPredefinedAction.FirstPage)
'添加按钮到文档
doc.Form.Fields.Add(button)
'从页面取回按钮的控件注释,把动作挂到它的 OnActivated 上
Dim annotations As PdfAnnotationCollection = PdfAnnotationCollection.Create(lastPage)
DirectCast(annotations(annotations.Count - 1), PdfWidgetAnnotation).OnActivated = namedAction
'保存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;
//实例化PdfXYZExplicitDestination对象,传入指定页码及页面位置
PdfXYZExplicitDestination destination = new PdfXYZExplicitDestination(doc.Pages[2], 0, 0);
//创建go to动作
PdfGoToAction goToAction = new PdfGoToAction(destination);
//添加按钮到文档
doc.Form.Fields.Add(button);
//从页面取回按钮的控件注释,把动作挂到它的 OnActivated 上
PdfAnnotationCollection annotations = PdfAnnotationCollection.Create(lastPage);
((PdfWidgetAnnotation)annotations[annotations.Count - 1]).OnActivated = goToAction;
//保存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
'实例化PdfXYZExplicitDestination对象,传入指定页码及页面位置
Dim destination As New PdfXYZExplicitDestination(doc.Pages(2), 0, 0)
'创建go to动作
Dim goToAction As New PdfGoToAction(destination)
'添加按钮到文档
doc.Form.Fields.Add(button)
'从页面取回按钮的控件注释,把动作挂到它的 OnActivated 上
Dim annotations As PdfAnnotationCollection = PdfAnnotationCollection.Create(lastPage)
DirectCast(annotations(annotations.Count - 1), PdfWidgetAnnotation).OnActivated = goToAction
'保存PDF文档
doc.SaveToFile("result.pdf", FileFormat.PDF)








