DEV Community


Add Navigation Buttons to PDF using a Free Java API

jazzzzz profile image Jane Updated on ・2 min read

When viewing a large PDF document, adding navigation buttons to it is a great time-saving method that allows readers to jump to a certain page from another page easily. This article will introduce how to create a button that redirects to a special page (for example, home page, last page and next page) or a particular page (for example, page 4) in PDF using Free Spire.PDF for Java.

Import jar dependency (2 Methods)
● Download the Free Spire.PDF for Java and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

Enter fullscreen mode Exit fullscreen mode

Adding Navigation Buttons

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.actions.PdfActionDestination;
import com.spire.pdf.actions.PdfGoToAction;
import com.spire.pdf.actions.PdfNamedAction;
import com.spire.pdf.fields.PdfButtonField;
import com.spire.pdf.general.PdfDestination;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class AddNavigationButton {

    public static void main(String[] args) throws Exception {

        //create a PdfDocument object and load a sample PDF file
        PdfDocument doc = new PdfDocument();

        //get the last page
        PdfPageBase lastPage = doc.getPages().get(doc.getPages().getCount() - 1);

        //allow creating forms in PDF

        //define float variables to specify the location and size of the button
        float x = 80;
        float y = 300;
        float width = 150;
        float height = 22;

        //create a truetype font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 13), true);

        //add a button that navigates to the first page
        PdfButtonField btn_1 = new PdfButtonField(lastPage, "btn_1");
        Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);
        btn_1.setText("Return to Page 1");
        btn_1.setBackColor(new PdfRGBColor(Color.ORANGE));
        btn_1.setForeColor(new PdfRGBColor(;
        btn_1.setBorderColor(new PdfRGBColor(;
        PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);

        //add a button that navigates to a particular page
        PdfButtonField btn_2 = new PdfButtonField(lastPage, "btn_2");
        rect = new Rectangle2D.Float( x, y + height + 5, width, height);
        btn_2.setText("Jump to Page 4");
        btn_2.setBackColor(new PdfRGBColor(Color.ORANGE));
        btn_2.setForeColor(new PdfRGBColor(;
        btn_2.setBorderColor(new PdfRGBColor(;
        PdfGoToAction goToAction = new PdfGoToAction(new PdfDestination(doc.getPages().get(3)));

        //save to file
        doc.saveToFile("NavigationButton.pdf", FileFormat.PDF);
Enter fullscreen mode Exit fullscreen mode

Navigation Buttons

Discussion (0)

Editor guide