Create Navigation Buttons in PDF in C#/VB.NET

A navigation button in a PDF document can redirect users from one page to another. For instance, when a navigation button is clicked, we can jump to the next/last page or return to the previous/first page. This article will introduce how to create such a navigation button in PDF using Spire.PDF.

Step 1: Initialize an instance of PdfDocument class and load a PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Sales Report.pdf");

Step 2: Enable creation of form fields in the document.

doc.AllowCreateForm = true;

Step 3: Get the last page.

PdfPageBase page = doc.Pages[doc.Pages.Count-1];

Step 4: Create a button field and specify the button name, tooltip and border style.

PdfButtonField button = new PdfButtonField(page, "Return to First Page");
button.Bounds = new RectangleF(page.ActualSize.Width/2-50, 400, 100, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ToolTip = "First Page";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 7f,PdfFontStyle.Bold);

Step 5: Create a PdfNamedAction that goes to the named destination (previous, next, first or last page) and add the action to the button field.

PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
button.Actions.GotFocus = namedAction;

Step 6: Add the button to the document.

doc.Form.Fields.Add(button);
doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF);

Output:

How to Create Navigation Buttons in PDF in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace NavigationButtons
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Sales Report.pdf");
            doc.AllowCreateForm = true;
            PdfPageBase page = doc.Pages[doc.Pages.Count - 1];

            PdfButtonField button = new PdfButtonField(page, "Return to First Page");
            button.Bounds = new RectangleF(page.ActualSize.Width / 2 - 50, 400, 100, 20);
            button.BorderColor = new PdfRGBColor(Color.AliceBlue);
            button.BorderStyle = PdfBorderStyle.Solid;
            button.ToolTip = "First Page";
            button.Font = new PdfFont(PdfFontFamily.Helvetica, 7f, PdfFontStyle.Bold);

            PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
            button.Actions.GotFocus = namedAction;

            doc.Form.Fields.Add(button);
            doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.Fields
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace NavigationButtons
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\Sales Report.pdf")
			doc.AllowCreateForm = True
			Dim page As PdfPageBase = doc.Pages(doc.Pages.Count - 1)

			Dim button As New PdfButtonField(page, "Return to First Page")
			button.Bounds = New RectangleF(page.ActualSize.Width / 2 - 50, 400, 100, 20)
			button.BorderColor = New PdfRGBColor(Color.AliceBlue)
			button.BorderStyle = PdfBorderStyle.Solid
			button.ToolTip = "First Page"
			button.Font = New PdfFont(PdfFontFamily.Helvetica, 7F, PdfFontStyle.Bold)

			Dim namedAction As New PdfNamedAction(PdfActionDestination.FirstPage)
			button.Actions.GotFocus = namedAction

			doc.Form.Fields.Add(button)
			doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF)
		End Sub
	End Class
End Namespace