在 pdf 文档中,超链接用于跳转到特定页面、外部网站或电子邮件地址,但随着内容的变更,超链接可能需要更新或删除。通过修改 pdf 中的超链接,用户可以确保链接指向最新的页面或移除已失效的链接,从而提升文档的可读性和专业性。本文介绍如何使用 spire.pdf for java 在 java 中更新或删除 pdf 中的超链接。
安装 spire.pdf for java
首先,您需要在 java 程序中添加 spire.pdf.jar 文件作为依赖项。jar 文件可以从此链接下载。如果您使用 maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 jar 文件。
com.e-iceblue
e-iceblue
https://repo.e-iceblue.cn/repository/maven-public/
e-iceblue
spire.pdf
11.2.3
java 更新 pdf 中的超链接
spire.pdf for java 提供了 pdftextweblinkannotationwidget.set 方法,可用于修改 pdf 中的超链接。以下是具体步骤:
- 创建 pdfdocument 对象。
- 使用 pdfdocument.loadfromfile() 方法加载 pdf 文件。
- 通过 pdfdocument.getpages().get() 方法获取文档的第一页。
- 调用 pdfpagebase.getannotationswidget() 方法获取页面的注释集合。
- 使用 pdfannotationcollection.get() 方法获取第一个注释并将其转换为 pdftextweblinkannotationwidget 对象。
- 通过 pdftextweblinkannotationwidget.set 方法修改其 url 地址。
- 使用 pdfdocument.savetofile() 方法保存修改后的 pdf 文件。
- java
import com.spire.pdf.pdfdocument;
import com.spire.pdf.pdfpagebase;
import com.spire.pdf.annotations.pdfannotationcollection;
import com.spire.pdf.annotations.pdftextweblinkannotationwidget;
public class updatehyperlinks {
public static void main(string[] args) {
// 创建 pdfdocument 对象
pdfdocument document = new pdfdocument();
// 加载 pdf 文件
document.loadfromfile("示例.pdf");
// 获取第一页
pdfpagebase page = document.getpages().get(0);
// 获取页面的注释集合
pdfannotationcollection widgetcollection = page.getannotationswidget();
// 获取集合内第一个超链接注释
pdftextweblinkannotationwidget uriwidget = (pdftextweblinkannotationwidget) widgetcollection.get(0);
// 修改超链接 url地址
uriwidget.set;
// 保存修改后的 pdf 文件
document.savetofile("更改超链接.pdf");
// 关闭 pdf 文档以释放资源
document.close();
document.dispose();
}
}
java 删除 pdf 中的超链接
spire.pdf for java 还提供了 pdfannotationcollection.remove() 方法,可用于删除 pdf 中的超链接。需要注意的是,删除超链接后,其外观仍会保留,但链接功能将被移除。以下是具体步骤:
- 创建 pdfdocument 对象。
- 使用 pdfdocument.loadfromfile() 方法加载 pdf 文件。
- 通过 pdfdocument.getpages().get() 方法获取文档的第一页。
- 调用 pdfpagebase.getannotationswidget() 方法获取页面的注释集合。
- 从后往前遍历注释集合,并检查当前注释是否为 pdftextweblinkannotationwidget 对象。
- 将当前注释转换为 pdftextweblinkannotationwidget 对象,并使用 pdfannotationcollection.remove() 方法将超链接注释从页面中删除。
- 使用 pdfdocument.savetofile() 方法保存修改后的 pdf 文件。
- java
import com.spire.pdf.pdfdocument;
import com.spire.pdf.pdfpagebase;
import com.spire.pdf.annotations.pdfannotation;
import com.spire.pdf.annotations.pdfannotationcollection;
import com.spire.pdf.annotations.pdftextweblinkannotationwidget;
public class removehyperlinks {
public static void main(string[] args) {
// 创建 pdfdocument 对象
pdfdocument document = new pdfdocument();
// 加载 pdf 文件
document.loadfromfile("示例.pdf");
// 获取第一页
pdfpagebase page = document.getpages().get(0);
// 获取页面的注释集合
pdfannotationcollection widgetcollection = page.getannotationswidget();
// 检查页面是否有注释
if (widgetcollection.getcount() > 0) {
// 反向遍历注释集合
for (int i = widgetcollection.getcount() - 1; i >= 0; i--) {
// 获取当前注释
pdfannotation annotation = widgetcollection.get(i);
// 检查注释是否为文本网页链接
if (annotation instanceof pdftextweblinkannotationwidget) {
// 将注释强制转换为文本网页链接注释
pdftextweblinkannotationwidget link = (pdftextweblinkannotationwidget) annotation;
// 从集合中删除文本网页链接注释
widgetcollection.remove(link);
}
}
}
// 保存修改后的文档到输出文件
document.savetofile("删除超链接.pdf");
// 关闭 pdf 文档以释放资源
document.close();
document.dispose();
}
}
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。