Jump to Specified Page or Location in PDF

When dealing with PDF file format, developers may need this: add a button to PDF; clicking on the button will navigate to specified page or location in PDF file. Spire.PDF, specially designed for developers can meet your need in this.

Playing an action executes the action’s recorded commands in the active document. Spire.PDF provides you an action called PDFGoToAction. PDFGoToAction can navigate to specified page or location in PDF file. Spire.PDF also enables developers to add annotations to PDF file. If the content of an annotation is PDFGoToAction, clicking on the area to display annotation invokes PDFGoToAction. This is how to jump to specified page or location in PDF file.

Step 1: Create a PDFGoToAction instance

[C#]
PDFDestination pageBottomDest = new PDFDestination(pagethree);
pageBottomDest.Location = new PointF(0, 5);
pageBottomDest.Mode = PDFDestinationMode.Location;
pageBottomDest.Zoom = 1f;
PDFGoToAction action = new PDFGoToAction(pageBottomDest);

The PDFDestination instance specifies the page number and the location in the page. In this sample code, "pagethree" is page number and (0, 5) specifies the location in "pagethree".

Step 2: Draw a rectangle. The rectangle specifies the area to display annotation content

[C#]
pageone.Canvas.DrawRectangle(PDFBrushes.DarkGray, buttonBounds);
pageone.Canvas.DrawString("To Last Page", buttonFont, PDFBrushes.CadetBlue, buttonBounds, format2);

Step 3: Create a PDFActionAnnotation instance

[C#]
PDFActionAnnotation annotation = new PDFActionAnnotation(buttonBounds, action);

"buttonBounds" specifies the area to display the content of annotation and "action" is bound to "annotation".

Step 4: Add the PDFActionAnnotation instance "annotation" to PDF file

[C#]
(pageone as PDFNewPage).Annotations.Add(annotation);

Effect screenshot:

Jump to Specified Page or Location in PDF

Jump to Specified Page or Location in PDF

Full code:

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

namespace GotoAction
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();

            PdfPageBase pageone = document.Pages.Add();
            pageone.Canvas.DrawString("This is Page One.",
                                   new PdfFont(PdfFontFamily.Helvetica, 20f),
                                   new PdfSolidBrush(Color.Black),
                                   10, 10);
            PdfPageBase pagetwo = document.Pages.Add();
            pagetwo.Canvas.DrawString("This is Page Two.",
                                   new PdfFont(PdfFontFamily.Helvetica, 20f),
                                   new PdfSolidBrush(Color.Black),
                                   10, 10);
            PdfPageBase pagethree = document.Pages.Add();
            pagethree.Canvas.DrawString("This is Page Three.",
                                   new PdfFont(PdfFontFamily.Helvetica, 20f),
                                   new PdfSolidBrush(Color.Black),
                                   10, 10);

            PdfDestination pageBottomDest = new PdfDestination(pagethree);
            pageBottomDest.Location = new PointF(0, 5);
            pageBottomDest.Mode = PdfDestinationMode.Location;
            pageBottomDest.Zoom = 1f;
            PdfGoToAction action = new PdfGoToAction(pageBottomDest);

            PdfTrueTypeFont buttonFont = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
            float buttonWidth = 70;
            float buttonHeight = buttonFont.Height * 1.5f;
            float x = pageone.Canvas.ClientSize.Width - buttonWidth - 100;
            PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            RectangleF buttonBounds = new RectangleF(x, 0, buttonWidth, buttonHeight);
            pageone.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds);
            pageone.Canvas.DrawString("To Last Page", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2);

            PdfActionAnnotation annotation = new PdfActionAnnotation(buttonBounds, action);
            annotation.Border = new PdfAnnotationBorder(0.75f);
            annotation.Color = Color.LightGray;
            (pageone as PdfNewPage).Annotations.Add(annotation);

            document.SaveToFile("result.pdf");

            System.Diagnostics.Process.Start("result.pdf");
        }

    }
}