Java 如何在PDF文档中添加超链接

 时间:2024-10-12 22:56:10

1、首先,需要将PDF Jar包-Free Spire.PDF for Java下载至本地。解压后,找到lib文件夹下的jar文件。方便下一步手动引入jar。

2、在IDEA中打开“Project Structure”界面,执行如下图步骤,手动将本地路径下的Spire.Pdf.jar文件导入到java应用程序。

Java 如何在PDF文档中添加超链接

3、勾选选项,点击“Apply”,完成引入jar到Java程序。

Java 如何在PDF文档中添加超链接

4、引用完成后,胆咣骜岱编辑如下代码,在PDF文档中添加不同类型的超链接。import com.spire.pdf.annotations.*;import com.spire.pdf.爿讥旌护graphics.*;import com.spire.pdf.*;import java.awt.*;import java.awt.font.TextAttribute;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.util.HashMap;public class AddLinksToPdf { public static void main(String[] args) throws Exception { //创建PDF文档 PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.getPages().add(); //初始化X,Y坐标 float y = 30; float x = 0; // 创建一个普通字体 PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true); //创建一个带下划线的字体 HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>(); hm.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); hm.put(TextAttribute.SIZE, 13); hm.put(TextAttribute.FAMILY, "Arial"); Font font = new Font(hm); PdfTrueTypeFont underlineFont = new PdfTrueTypeFont(font,true); //添加简单链接到PDF String label = "简单链接: "; PdfStringFormat format = new PdfStringFormat(); format.setMeasureTrailingSpaces(true); page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y,format); x = (float)plainFont.measureString(label,format).getWidth(); page.getCanvas().drawString("超链接地址", underlineFont, PdfBrushes.getBlue(), x, y+2); y = y + 26; //添加超文本链接到PDF label= "超文本链接: "; page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format); x = (float)plainFont.measureString(label,format).getWidth(); PdfTextWebLink webLink = new PdfTextWebLink(); webLink.setText("超链接文本"); webLink.setUrl("链接地址"); webLink.setFont(plainFont); webLink.setBrush(PdfBrushes.getBlue()); webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y)); y= y + 26; //添加邮箱链接到PDF label = "邮箱链接: "; page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format); x = (float)plainFont.measureString(label, format).getWidth(); webLink = new PdfTextWebLink(); webLink.setText("联系我们"); webLink.setUrl("邮件地址"); webLink.setFont(plainFont); webLink.setBrush(PdfBrushes.getBlue()); webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y)); y = y + 26; //添加文档链接到PDF label = "文档链接: "; page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format); x = (float)plainFont.measureString(label, format).getWidth(); page.getCanvas().drawString("详情参阅原文件", plainFont, PdfBrushes.getBlue(), x, y, format); Rectangle2D rect = new Rectangle2D.Float(x,y+2,60,15); PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(rect,"C:\\Users\\Administrator\\Desktop\\测试文件.docx"); fileLinkAnnotation.setBorder(new PdfAnnotationBorder(0f)); ((PdfNewPage) ((page instanceof PdfNewPage) ? page : null)).getAnnotations().add(fileLinkAnnotation); //保存文档 doc.saveToFile("result.pdf"); doc.close(); }}

5、执行以上代码,生成PDF文档。可在生成的文档中查看创建的超链接效果。

  • 养猪都有哪些圈舍?
  • 如何连接宽带网络
  • 如何使jsp加载本地视频
  • MyEclipse定制工具栏和菜单
  • 热门搜索
    关于三国演义的手抄报 走进田园手抄报 有关安全的手抄报图片 感恩老师的手抄报内容 英语节日手抄报图片 科技报手抄报 初中交通安全手抄报 心里手抄报 放飞梦想手抄报图片 手抄报英语