在PDF 中可經過按鈕來添加動做跳轉到指定頁面,包括跳轉到文檔首頁、文檔末頁、跳轉到上一頁、下一頁、或跳轉到指定頁面等。下面將經過java代碼來演示如何添加具備以上幾種功能的按鈕。html
使用工具:java
Jar文件獲取及導入:maven
方法1:經過官網下載jar文件包。下載後,解壓文件,並將lib文件夾下的Spire.Pdf.jar文件導入java程序。參考以下導入效果:工具
方法2: 可經過maven倉庫安裝導入。字體
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{ //建立文檔,添加頁面 PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("test.pdf"); //分別獲取第一頁、第二頁及最後一頁 PdfPageBase page0 = pdf.getPages().get(0); PdfPageBase page1 = pdf.getPages().get(1); PdfPageBase page2 = pdf.getPages().get(pdf.getPages().getCount()-1); //設置容許建立窗體 pdf.setAllowCreateForm(true); //定義float變量來肯定按鈕的位置及大小 float x = 480; float y = 750; float width = 70; float height = 23; //建立truetype字體 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("楷體", Font.PLAIN, 9), true); //建立按鈕0跳轉到最後一頁 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("跳轉至最後一頁"); 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(PdfActionDestination.LastPage); btn_0.getActions().setMouseDown(namedAction); pdf.getForm().getFields().add(btn_0); //建立按鈕1跳轉到上一頁 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("上一頁"); 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); //建立按鈕2跳轉下一頁 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("下一頁"); 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); //建立按鈕3跳轉到首頁 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("返回首頁"); 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); //建立按鈕4跳轉到指定頁 PdfButtonField btn_4 = new PdfButtonField(page2, "btn_4"); rect = new Rectangle2D.Float( x,90 , width, height); btn_4.setBounds(rect); btn_4.setText("跳轉至第二頁"); 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); //保存文檔 pdf.saveToFile("NavigationButton.pdf", FileFormat.PDF); pdf.close(); } }
按鈕添加效果:spa
(本文完)code