使用工具:
•Free Spire.PDF for Java (免费版)
•IntelliJ IDEA
Jar文件获取及导入:
方法1:通过官网下载jar文件包。下载后,解压文件,并将lib文件夹下的Spire.Pdf.jar文件导入java程序。参考如下导入效果:
方法2: 可通过maven仓库安装导入。
Java 代码示例
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(); } }
按钮添加效果: