DEV Community

CodeSharing
CodeSharing

Posted on • Updated on

Add Navigation Buttons to PDF using a Free Java API

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.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
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 com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.graphics.PdfTrueTypeFont;

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();
        doc.loadFromFile("input.pdf");

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

        //allow creating forms in PDF
        doc.setAllowCreateForm(true);

        //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.setBounds(rect);
        btn_1.setFont(font);
        btn_1.setText("Return to Page 1");
        btn_1.setBackColor(new PdfRGBColor(Color.ORANGE));
        btn_1.setForeColor(new PdfRGBColor(Color.blue));
        btn_1.setBorderColor(new PdfRGBColor(Color.blue));
        PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
        btn_1.getActions().setMouseDown(namedAction);
        doc.getForm().getFields().add(btn_1);

        //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.setBounds(rect);
        btn_2.setText("Jump to Page 4");
        btn_2.setFont(font);
        btn_2.setBackColor(new PdfRGBColor(Color.ORANGE));
        btn_2.setForeColor(new PdfRGBColor(Color.blue));
        btn_2.setBorderColor(new PdfRGBColor(Color.blue));
        PdfGoToAction goToAction = new PdfGoToAction(new PdfDestination(doc.getPages().get(3)));
        btn_2.getActions().setMouseDown(goToAction);
        doc.getForm().getFields().add(btn_2);

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

Output
Navigation Buttons

Top comments (0)