In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to add the page jump button function to Java in PDF. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Use the tool:
Free Spire.PDF for Java (free version) IntelliJ IDEA
Jar file acquisition and import:
Method 1: download the jar package through the official website. After downloading, extract the file and import the Spire.Pdf.jar file under the lib folder into the java program. Refer to the following import effect:
Method 2: it can be installed and imported through maven warehouse.
Java code example
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 the document and add the page PdfDocument pdf = new PdfDocument () Pdf.loadFromFile ("test.pdf"); / / get the first, second and last pages PdfPageBase page0 = pdf.getPages (). Get (0); PdfPageBase page1 = pdf.getPages (). Get (1); PdfPageBase page2 = pdf.getPages (). Get (pdf.getPages (). GetCount ()-1); / / set allow form pdf.setAllowCreateForm (true) / / define the float variable to determine the position and size of the button float x = 480; float y = 750; float width = 70; float height = 23; / / create the truetype font PdfTrueTypeFont font = new PdfTrueTypeFont ("italics", Font.PLAIN, 9), true); / / create button 0 to jump to the last page PdfButtonField btn_0 = 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 last page"); btn_0.setBackColor (new PdfRGBColor (245245245)); btn_0.setForeColor (new PdfRGBColor (Color.black)); btn_0.setBorderColor (new PdfRGBColor (Color.white)); PdfNamedAction namedAction = new PdfNamedAction (PdfActionDestination.LastPage) Btn_0.getActions () .setMouseDown (namedAction); pdf.getForm () .getFields () .add (btn_0); / / create button 1 to jump to the previous page PdfButtonField btn_1 = 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 page") Btn_1.setBackColor (new PdfRGBColor (Color.white)); btn_1.setForeColor (new PdfRGBColor (Color.black)); btn_1.setBorderColor (new PdfRGBColor (245245245)); namedAction = new PdfNamedAction (PdfActionDestination.PrevPage); btn_1.getActions (). SetMouseDown (namedAction); pdf.getForm (). GetFields (). Add (btn_1); / / create button 2 to jump to the next page PdfButtonField btn_2 = 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 page"); btn_2.setBackColor (new PdfRGBColor (245245245)); 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 to jump to the home page PdfButtonField btn_3 = new PdfButtonField (page2, "btn_3"); rect = new Rectangle2D.Float (x, 60, width, height); btn_3.setBounds (rect); btn_3.setFont (font); btn_3.setText ("return to home") Btn_3.setBackColor (new PdfRGBColor (245245245)); 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 the specified page PdfButtonField btn_4 = new PdfButtonField (page2, "btn_4") Rect = new Rectangle2D.Float (XBE 90, width, height); btn_4.setBounds (rect); btn_4.setText ("Jump to Page 2"); btn_4.setFont (font); btn_4.setBackColor (new PdfRGBColor (Color.white)); btn_4.setForeColor (new PdfRGBColor (Color.black)); btn_4.setBorderColor (new PdfRGBColor (245245245)); PdfGoToAction goToAction = new PdfGoToAction (new PdfDestination (pdf.getPages (). Get (1) Btn_4.getActions () .setMouseDown (goToAction); pdf.getForm () .getFields () .add (btn_4); / / Save document pdf.saveToFile ("NavigationButton.pdf", FileFormat.PDF); pdf.close () }} this is the end of this article on "how to add page jump button function to Java in PDF". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.