pdf 文档中的超链接可以帮助用户快速跳转到指定页面或打开相关文档,让 pdf 文件更加便捷、易用。但如果链接目标发生变化,或者链接指向了错误的页面,就可能给文档使用者带来困扰或误解。
因此,及时修改或删除 pdf 文档中的错误或无效超链接,对于确保链接的准确性和可用性非常重要,也能为用户提供更好的阅读体验。本文将介绍如何通过 .net 程序更改或删除 pdf 文档中的超链接。
安装 pdf 处理库
首先,需要将 pdf 处理库中的 dll 文件添加为 .net 项目的引用。相关 dll 文件可以从官方站点下载,也可以通过 nuget 进行安装。
pm> install-package spire.pdf
更改 pdf 中超链接的 url
要更改 pdf 页面中超链接的 url,需要从页面的注释集合中获取超链接注释,将其转换为 pdflinkannotation 对象,然后通过其 pdfuriaction 操作的 uri 属性重新设置 url。具体步骤如下:
- 创建一个 pdfdocument 类的对象。
- 使用 pdfdocument.loadfromfile() 方法加载 pdf 文件。
- 使用 pdfdocument.pages[] 属性获取文档的第一页。
- 使用 pdfpagebase.annotationsv2[] 属性获取页面中的第一个超链接注释,并将其转换为 pdflinkannotation 对象。
- 通过 pdflinkannotation 对象的 pdfuriaction 操作中的 uri 属性重新设置超链接的 url。
- 使用 pdfdocument.savetofile() 方法保存文档。
完整示例代码如下:
using spire.pdf;
using spire.pdf.annotations;
using system;
namespace changehyperlink
{
internal class program
{
static void main(string[] args)
{
// 创建一个 pdfdocument 对象
pdfdocument pdf = new pdfdocument();
// 加载 pdf 文件
pdf.loadfromfile("sample.pdf");
// 获取第一页
pdfpagebase page = pdf.pages[0];
// 获取第一个超链接
pdfuriannotationwidget url = (pdfuriannotationwidget)page.annotations[0];
// 重新设置超链接的 url
url.uri = "https://en.wikipedia.org/wiki/climate_change";
// 保存 pdf 文件
pdf.savetofile("changehyperlink.pdf");
pdf.dispose();
}
}
}从 pdf 中删除超链接
pdf 处理库提供了 pdfpagebase.annotationsv2.removeat() 方法,可以根据索引删除 pdf 页面中的指定超链接。
如果需要删除 pdf 文档中的所有超链接,则需要遍历文档中的各个页面,获取每个页面的注释集合,检查注释是否为 pdflinkannotation 类的实例,如果是,则将其删除。具体步骤如下:
- 创建一个 pdfdocument 类的对象。
- 使用 pdfdocument.loadfromfile() 方法加载 pdf 文档。
- 如果需要删除指定的超链接,获取包含该超链接的页面,并使用 pdfpagebase.annotationsv2.removeat() 方法根据索引删除该超链接。
- 如果需要删除所有超链接,遍历文档中的各个页面,并通过 pdfpagebase.annotationsv2 属性获取每个页面的注释集合。
- 检查注释是否为 pdflinkannotation 类的实例。如果是,则使用 pdfannotationcollection.remove(pdflinkannotation) 方法删除该注释。
- 使用 pdfdocument.savetofile() 方法保存文档。
完整示例代码如下:
// 示例 1:更改 pdf 中超链接的 url
using spire.pdf;
using spire.pdf.actions;
using spire.pdf.interactive.annotations;
namespace changehyperlink
{
internal class program
{
static void main(string[] args)
{
// 创建一个 pdfdocument 对象
pdfdocument pdf = new pdfdocument();
// 加载 pdf 文件
pdf.loadfromfile("sample.pdf");
// 获取第一页
pdfpagebase page = pdf.pages[0];
// 获取第一个超链接
pdflinkannotation url = page.annotationsv2[0] as pdflinkannotation;
// 重新设置超链接的 url
(url.action as pdfuriaction).uri = "https://en.wikipedia.org/wiki/climate_change";
// 保存 pdf 文件
pdf.savetofile("changehyperlink.pdf");
pdf.dispose();
}
}
}
// 示例 2:删除 pdf 中的超链接
using spire.pdf;
using spire.pdf.interactive.annotations;
namespace deletehyperlink
{
internal class program
{
static void main(string[] args)
{
// 创建一个 pdfdocument 对象
pdfdocument pdf = new pdfdocument();
// 加载 pdf 文件
pdf.loadfromfile("sample.pdf");
// 删除第一页中的第二个超链接
//pdfpagebase page = pdf.pages[0];
//page.annotationsv2.removeat(1);
// 删除文档中的所有超链接
// 遍历文档中的各个页面
foreach (pdfpagebase page in pdf.pages)
{
// 获取页面的注释集合
pdfannotationcollection collection = page.annotationsv2;
for (int i = collection.count - 1; i >= 0; i--)
{
pdfannotation annotation = collection[i];
// 检查注释是否为 pdflinkannotation 的实例
if (annotation is pdflinkannotation)
{
pdflinkannotation url = (pdflinkannotation)annotation;
// 删除超链接
collection.remove(url);
}
}
}
// 保存文档
pdf.savetofile("deletehyperlink.pdf");
pdf.dispose();
}
}
}知识扩展
在 c# 中修改或删除 pdf 的超链接,核心操作对象是 pdf 中的链接注释(link annotation)。不同库的 api 差异较大,下面按库分别给出完整实现。
方案一:spire.pdf(api 最简洁,中文文档完善)
spire.pdf 提供了最直观的 api:pdfuriannotationwidget.uri 用于修改 url,pdfpagebase.annotationswidget.removeat() 用于删除。
安装:install-package spire.pdf
修改超链接
using spire.pdf;
using spire.pdf.annotations;
public static void changehyperlink(string inputpath, string outputpath, int pageindex, int linkindex, string newurl)
{
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile(inputpath);
pdfpagebase page = pdf.pages[pageindex];
pdfuriannotationwidget url = (pdfuriannotationwidget)page.annotations[linkindex];
url.uri = newurl;
pdf.savetofile(outputpath);
pdf.dispose();
}删除指定超链接
csharp
public static void removehyperlink(string inputpath, string outputpath, int pageindex, int linkindex)
{
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile(inputpath);
pdfpagebase page = pdf.pages[pageindex];
page.annotationswidget.removeat(linkindex);
pdf.savetofile(outputpath);
pdf.dispose();
}删除文档中所有超链接
public static void removeallhyperlinks(string inputpath, string outputpath)
{
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile(inputpath);
for (int i = 0; i < pdf.pages.count; i++)
{
pdfpagebase page = pdf.pages[i];
for (int j = page.annotationswidget.count - 1; j >= 0; j--)
{
if (page.annotationswidget[j] is pdfuriannotationwidget)
{
page.annotationswidget.removeat(j);
}
}
}
pdf.savetofile(outputpath);
pdf.dispose();
}注意:遍历删除时必须从后往前(j--),否则索引会错乱。
方案二:itext7(开源 agpl,控制力最强)
itext7 通过 pdflinkannotation 类操作链接注释。修改链接使用 setaction() 或 setdestination(),删除则直接从页面的注释集合中移除。
安装:install-package itext7
修改外部链接 url
using itext.kernel.pdf;
using itext.kernel.pdf.annot;
using itext.kernel.pdf.action;
public static void changelinkuri(string inputpath, string outputpath, string olduri, string newuri)
{
using (pdfdocument pdfdoc = new pdfdocument(new pdfreader(inputpath), new pdfwriter(outputpath)))
{
for (int i = 1; i <= pdfdoc.getnumberofpages(); i++)
{
pdfpage page = pdfdoc.getpage(i);
var annotations = page.getannotations();
foreach (var annot in annotations)
{
if (annot is pdflinkannotation link)
{
pdfaction action = link.getaction();
if (action != null && action.gettype() == pdfaction.uri)
{
string currenturi = action.getpdfobject().getasstring(pdfname.uri)?.tostring();
if (currenturi == olduri)
{
link.setaction(pdfaction.createuri(newuri));
}
}
}
}
}
}
}删除所有链接注释
public static void removealllinks(string inputpath, string outputpath)
{
using (pdfdocument pdfdoc = new pdfdocument(new pdfreader(inputpath), new pdfwriter(outputpath)))
{
for (int i = 1; i <= pdfdoc.getnumberofpages(); i++)
{
pdfpage page = pdfdoc.getpage(i);
// 反向遍历并移除所有链接注释
var annots = page.getannotations();
for (int j = annots.count - 1; j >= 0; j--)
{
if (annots[j] is pdflinkannotation)
{
page.removeannotation(annots[j]);
}
}
}
}
}方案三:aspose.pdf(商业库,功能最全)
aspose.pdf 通过 linkannotation 类操作链接,删除时使用 page.annotations.delete(a)。它还能同时处理底层文本的视觉格式(去除下划线和蓝色)。
安装:install-package aspose.pdf
修改外部链接
using aspose.pdf;
using aspose.pdf.annotations;
public static void modifylinkaspose(string inputpath, string outputpath, string newuri)
{
document doc = new document(inputpath);
foreach (var page in doc.pages)
{
foreach (annotation a in page.annotations)
{
if (a.annotationtype == annotationtype.link)
{
linkannotation la = (linkannotation)a;
if (la.action is gotouriaction)
{
la.action = new gotouriaction(newuri);
}
}
}
}
doc.save(outputpath);
}删除链接并清除视觉格式
public static void removelinkwithvisualcleanup(string inputpath, string outputpath)
{
document doc = new document(inputpath);
foreach (var page in doc.pages)
{
for (int i = page.annotations.count - 1; i >= 0; i--)
{
annotation a = page.annotations[i];
if (a.annotationtype == annotationtype.link)
{
// 清除底层文本的下划线和蓝色
textfragmentabsorber tfa = new textfragmentabsorber();
tfa.textsearchoptions = new textsearchoptions(a.rect);
page.accept(tfa);
foreach (textfragment tf in tfa.textfragments)
{
tf.textstate.underline = false;
tf.textstate.foregroundcolor = aspose.pdf.color.black;
}
page.annotations.delete(a);
}
}
}
doc.save(outputpath);
}方案四:syncfusion pdf(商业库,有社区版)
syncfusion 使用 pdfloadeddocument 加载文档,通过 pdfuriannotation 操作链接。修改链接时直接设置 uri 属性,删除时调用 remove()。
安装:install-package syncfusion.pdf.net.core
using syncfusion.pdf;
using syncfusion.pdf.interactive;
using syncfusion.pdf.parsing;
public static void modifylinkssyncfusion(string inputpath, string outputpath, string newuri)
{
using (pdfloadeddocument doc = new pdfloadeddocument(inputpath))
{
foreach (pdfloadedpage page in doc.pages)
{
foreach (pdfannotation annot in page.annotations)
{
if (annot is pdfuriannotation uriannot)
{
uriannot.uri = newuri;
}
}
}
doc.save(outputpath);
}
}
public static void removealllinkssyncfusion(string inputpath, string outputpath)
{
using (pdfloadeddocument doc = new pdfloadeddocument(inputpath))
{
foreach (pdfloadedpage page in doc.pages)
{
for (int i = page.annotations.count - 1; i >= 0; i--)
{
if (page.annotations[i] is pdfuriannotation)
{
page.annotations.removeat(i);
}
}
}
doc.save(outputpath);
}
}总结
本文介绍了如何使用 c# 和 .net 程序修改或删除 pdf 文档中的超链接。通过获取 pdf 页面中的超链接注释,可以重新设置超链接的 url;如果需要删除超链接,则可以根据索引删除指定链接,或遍历文档中的页面和注释集合,批量删除所有超链接。
这种方法适用于维护 pdf 文档中的链接内容,帮助及时更新失效或错误的链接,并保持文档的准确性和可用性。
到此这篇关于c#代码实现更改或删除pdf中的超链接的文章就介绍到这了,更多相关c#更改或删除pdf超链接内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论