您的位置:首页 > 手游攻略 > C#代码实现方式更改或删除PDF中的超链接实用指南

C#代码实现方式更改或删除PDF中的超链接实用指南

作者:互联网  时间: 2026-09-30 11:00:02  

平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“C#代码实现方法更改或删除PDF中的超链接”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。

在这个场景下,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超链接适合结合实际项目边做边理解。先抓住核心思路,再逐步补上细节和边界处理,最后效果会更稳定,也更容易复用。

最新游戏

更多

Copyright©2010-2019. All rights reserved | 波波三国游戏官网|[email protected]

备案编号:湘ICP备2022015115号-4