In the PDF can be added via the action button to jump to a specific page, including the jump to the page of the document, the document last page, jump to the previous page, next page, or jump to a specific page and so on. The following shows how to add a button to have more than several functions through java code.
use tools:
- Free Spire.PDF for Java (free version)
- IntelliJ IDEA
Jar file acquisition and import:
Method 1 : through the official website to download jar package. After the download, unzip the file and the lib folder under the Spire.Pdf.jar file into java program. Import reference to the following effects:
Method 2 : by mounting introducing maven repository .
Java code examples
import com.spire.pdf.*; import com.spire.pdf.actions.*; 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 Buttons { public static void main (String [] args) throws Exception { // create a document, add pages PDFDocument pdf = new new PDFDocument (); pdf.loadFromFile("test.pdf"); // respectively acquiring a first page, second page, and the last one PdfPageBase PAGE0 = pdf.getPages () GET (0. ); PdfPageBase page1 = pdf.getPages().get(1); PdfPageBase page2 = pdf.getPages().get(pdf.getPages().getCount()-1); // set allows you to create a form pdf.setAllowCreateForm ( to true ); // define float variables to determine the position and size of a button float X = 480 ; float Y = 750 ; float width = 70 ; float height = 23 is ; // create truetype font PdfTrueTypeFont font = new new PdfTrueTypeFont ( new new the Font ( "italics", Font.PLAIN, 9), to true ); // Create button 0 to jump to the last page PdfButtonField btn_0 = new new PdfButtonField (PAGE0, "btn_0" ); Rectangle2D.Float rect = new Rectangle2D.Float(x, y + 15, width, height); btn_0.setBounds(rect); btn_0.setFont(font); btn_0.setText ( "jump to the last page" ); btn_0.setBackColor(new PdfRGBColor(245,245,245)); btn_0.setForeColor(new PdfRGBColor(Color.black)); btn_0.setBorderColor(new PdfRGBColor(Color.white)); PdfNamedAction namedAction = new PdfNamedAction (DFA DictionaryReply long and thorough ti o n.LastPage); btn_0.getActions().setMouseDown(namedAction); pdf.getForm().getFields().add(btn_0); // Create a button 1 to jump to the previous PdfButtonField btn_1 = new new PdfButtonField (PAGE1, "btn_1" ); rect = new Rectangle2D.Float(x, y, width, height); btn_1.setBounds(rect); btn_1.setFont(font); btn_1.setText ( "Previous" ); btn_1.setBackColor(new PdfRGBColor(Color.white)); btn_1.setForeColor(new PdfRGBColor(Color.black)); btn_1.setBorderColor(new PdfRGBColor(245,245,245)); namedAction = new PdfNamedAction(PdfActionDestination.PrevPage); btn_1.getActions().setMouseDown(namedAction); pdf.getForm().getFields().add(btn_1); // Create button Jump 2 Next PdfButtonField btn_2 = new new PdfButtonField (PAGE1, "btn_2" ); rect = new Rectangle2D.Float(x, y + height + 5, width, height); btn_2.setBounds(rect); btn_2.setFont(font); btn_2.setText ( "Next" ); btn_2.setBackColor(new PdfRGBColor(245,245,245)); btn_2.setForeColor(new PdfRGBColor(Color.black)); btn_2.setBorderColor(new PdfRGBColor(Color.white)); namedAction = new PdfNamedAction(PdfActionDestination.NextPage); btn_2.getActions().setMouseDown(namedAction); pdf.getForm().getFields().add(btn_2); // Create button 3 goto PdfButtonField btn_3 = new new PdfButtonField (PAGE2, "btn_3" ); rect = new Rectangle2D.Float(x, 60, width, height); btn_3.setBounds(rect); btn_3.setFont(font); btn_3.setText ( "Home" ); btn_3.setBackColor(new PdfRGBColor(245,245,245)); btn_3.setForeColor(new PdfRGBColor(Color.black)); btn_3.setBorderColor(new PdfRGBColor(Color.white)); namedAction = new PdfNamedAction(PdfActionDestination.FirstPage); btn_3.getActions().setMouseDown(namedAction); pdf.getForm().getFields().add(btn_3); // Create button 4 to jump to a specific page PdfButtonField btn_4 = new new PdfButtonField (PAGE2, "btn_4" ); rect = new Rectangle2D.Float( x,90 , width, height); btn_4.setBounds(rect); btn_4.setText ( "Go to the second page" ); btn_4.setFont(font); btn_4.setBackColor(new PdfRGBColor(Color.white)); btn_4.setForeColor(new PdfRGBColor(Color.black)); btn_4.setBorderColor(new PdfRGBColor(245,245,245)); PdfGoToAction goToAction = new PdfGoToAction(new PdfDestination(pdf.getPages().get(1))); btn_4.getActions (). setMouseDown (goToAction); pdf.getForm().getFields().add(btn_4); // Save the document pdf.saveToFile ( "NavigationButton.pdf" , FileFormat.PDF); pdf.close(); } }
Button to add the effect:
(This article End)