栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 添加、更新和移除PDF超链接的实现方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 添加、更新和移除PDF超链接的实现方法

简介

PDF超链接用一个简单的链接包含了大量的信息,满足了人们在不占用太多空间的情况下渲染外部信息的需求。下面将介绍通过Java 在PDF中添加、更新和移除超链接。

(一)工具使用:

•  Free Spire.PDF for Java 2.4.4(免费版)
• Intellij IDEA

(二)导入Jar文件包:
•  方式一:首先,从官网获取Free Spire.PDF for Java文件包。

Step 1: 下载控件包之后解压,打开“Project Structure”界面。(以下是三种在IDEA中快速打开Project Structure界面的方式,可选其中任意一种)

Step 2:按以下操作步骤进行导入。① 选择“Modules”—“Dependencies”,添加外置jar包;② 进入"Attach File or Directories"界面选择jar文件路径,然后点击“OK”;③ 勾选jar路径选项,点击”OK”/”Apply”;④ 导入完成。如下图:


•  方式二:使用Maven配置导包。可以参考导入方法。

Java代码示例参考

(一) 添加超链接到PDF

添加命名空间:

import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.geom.*;
import java.util.HashMap;

1. 添加超文本连接

public class Textlink {
 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 hm = new HashMap();
 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();
 //创建PdfTextWeblink对象
 PdfTextWeblink weblink = new PdfTextWeblink();
 //设置超链接文本
 weblink.setText("主页");
 //设置超链接地址
 weblink.setUrl("https://www.google.com");
 //设置超链接字体和字体颜色
 weblink.setFont(plainFont);
 weblink.setBrush(PdfBrushes.getBlue());
 //添加超链接到页面
 weblink.drawTextWeblink(page.getCanvas(), new Point2D.Float(x, y));
 y= y +40;
 //保存文档
 doc.saveToFile("Addlinks.pdf");
 doc.close();
 }
}

添加结果:

2. 添加邮箱链接

public class EMaillink {
 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 hm = new HashMap();
 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);
 //添加邮箱链接
 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();
 //创建PdfTextWeblink对象
 PdfTextWeblink weblink = new PdfTextWeblink();
 weblink = new PdfTextWeblink();
 //设置超链接文本
 weblink.setText("联系我们");
 //设置超链接地址
 weblink.setUrl("mailto:123@qq.com");
 //设置超链接字体和字体颜色
 weblink.setFont(plainFont);
 weblink.setBrush(PdfBrushes.getBlue());
 //添加超链接到页面
 weblink.drawTextWeblink(page.getCanvas(), new Point2D.Float(x, y));
 y = y + 40;

 //保存文档
 doc.saveToFile("Addlinks.pdf");
 doc.close();
 }
}

添加结果:

3.   添加文档链接

public class Filelink {
 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 hm = new HashMap();
 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("打开文件", plainFont, PdfBrushes.getBlue(), x, y, format);
 Rectangle2D rect = new Rectangle2D.Float(x,y+10,60,15);
 //创建一个文件超链接对象并加载文件
 PdfFilelinkAnnotation filelinkAnnotation = new PdfFilelinkAnnotation(rect,"C:\Users\Administrator\Desktop\Sample.pdf");
 filelinkAnnotation.setBorder(new PdfAnnotationBorder(0f));
 //添加文件到超链接
 ((PdfNewPage) ((page instanceof PdfNewPage) ? page : null)).getAnnotations().add(filelinkAnnotation);
 //保存文档
 doc.saveToFile("Addlinks.pdf");
 doc.close();
 }
}

添加结果:

(二) 更新和移除超链接

      测试文档:

  

  使用PDFAnnotatioCollection 类和PdfTextWeblinkAnnotationWidget类创建超链注释集合并获取到第一个超链接,使用getUrl ()方法设置超链接地址,removeAt()方法移除超链接。

import com.spire.pdf.Pdfdocument;
import com.spire.pdf.PdfPagebase;
import com.spire.pdf.annotations.PdfAnnotationCollection;
import com.spire.pdf.annotations.PdfTextWeblinkAnnotationWidget;
public class UpdateDellinks {
 public static void main(String[] args) throws Exception {
 //创建PDF文档
 Pdfdocument doc = new Pdfdocument();
 //加载PDF源文件
 doc.loadFromFile("data/Addlinks.pdf");
 //获取文档第一页
 PdfPagebase page = doc.getPages().get(0);
 //获取第一页超链接注释的集合
 PdfAnnotationCollection annotationCollection = page.getAnnotationsWidget();
 //获取第一个超链接
 PdfTextWeblinkAnnotationWidget uriAnnotationWidget = (PdfTextWeblinkAnnotationWidget) annotationCollection.get(0);
 //设置超链接
 uriAnnotationWidget.setUrl("www.baidu.com");
 //removeAt()方法移除第二条超链接
 annotationCollection.removeAt(1);
 //保存文件
 doc.saveToFile("Output.pdf");
 }
}

更新移除结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号