How to open a PDF document at a specific page in C#, VB.NET

Generally, when we open a PDF document from a PDF viewer, it displays the first page instead of others. For some reasons, we may want to skip the first few pages and start on another page. This article will introduce how to specify a particular page when viewing a PDF document in C# and VB.NET.

Code Snippet:

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

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

Step 2: Create a PdfGoToAction which will redirect to a destination (page 2 in this case) in the current document.

PdfDestination destination = new PdfDestination(doc.Pages[1]);
PdfGoToAction action = new PdfGoToAction(destination);

Step 3: Sets the action to execute after the document is opened.

doc.AfterOpenAction = action;

Step 4: Save the file.

doc.SaveToFile("GoTo2Page.pdf",FileFormat.PDF);

Output:

The document opens at the second page.

How to open a PDF document at a specific page in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;

namespace OpenPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            PdfDestination destination = new PdfDestination(doc.Pages[1]);
            PdfGoToAction action = new PdfGoToAction(destination);
            action.Destination.Zoom = 0.8F;
            doc.AfterOpenAction = action;

            doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.General

Namespace OpenPDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

			Dim destination As New PdfDestination(doc.Pages(1))
			Dim action As New PdfGoToAction(destination)
			action.Destination.Zoom = 0.8F
			doc.AfterOpenAction = action

			doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF)
		End Sub
	End Class
End Namespace