C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#超链接插入PDF

C#代码实现将超链接插入到PDF的现有文本中

作者:2501_93070778

PDF 中的超链接是一项非常实用的功能,能够让读者快速、便捷地访问指定的网页,本文将介绍如何使用 Spire.PDF for .NET为 PDF 文档中的现有文本添加超链接,感兴趣的小伙伴可以了解下

PDF 中的超链接是一项非常实用的功能,能够让读者快速、便捷地访问指定的网页。通过在 PDF 文档中添加超链接,可以为读者提供更多补充信息,或引导他们前往相关的参考资源。当读者点击超链接时,对应的网页会立即在浏览器中打开。

本文将介绍如何使用 Spire.PDF for .NET,通过 .NET 程序为 PDF 文档中的现有文本添加超链接。

安装 Spire.PDF for .NET

首先,需要将 Spire.PDF for .NET 包中包含的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过链接直接下载,也可以通过 NuGet 进行安装。

PM> Install-Package Spire.PDF

使用 C#/VB.NET 在 PDF 现有文本上插入超链接

在 PDF 文档中,超链接是以**注释(Annotation)**的形式添加到页面上的。要在 PDF 的已有文本上插入超链接,首先需要定位目标文本;获取其所在位置后,即可创建一个包含链接的 PdfUriAnnotation 对象,并将其添加到对应位置。具体步骤如下:

具体示例代码如下:

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using System.Collections.Generic;
using System.Drawing;
using TextFindParameter = Spire.Pdf.Texts.TextFindParameter;

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];

            // 创建 PdfTextFinder 对象并设置查找选项
            PdfTextFinder finder = new PdfTextFinder(page);
            finder.Options.Parameter = TextFindParameter.IgnoreCase;

            // 在页面中查找指定文本,并获取第三次出现的位置
            List<PdfTextFragment> collection = finder.Find("climate change");
            PdfTextFragment fragment = collection[2];

            // 遍历该文本出现位置的所有文本边界
            foreach (RectangleF bounds in fragment.Bounds)
            {
                // 创建一个超链接注释
                PdfUriAnnotation url = new PdfUriAnnotation(bounds);
                // 设置超链接的 URL
                url.Uri = "https://en.wikipedia.org/wiki/Climate_change";
                // 设置超链接注释的边框
                url.Border = new PdfAnnotationBorder(1f);
                // 设置边框颜色
                url.Color = Color.Blue;
                // 将超链接注释添加到页面中
                page.Annotations.Add(url);
            }

            // 保存 PDF 文件
            pdf.SaveToFile("AddHyperlinks.pdf");
            pdf.Dispose();
        }
    }
}

方法补充

C#插入超链接到PDF文档的三种方法

1.链接到外部网页

//创建PDF文档并添加一页
PdfDocument pdf = new PdfDocument();
PdfPageBase page =pdf.Pages.Add();
 
//定义坐标变量并赋初值
float x = 10;
float y = 50;
 
//创建字体
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true);
//添加文本到页面
string text = "更多详情请访问E-iceblue官方网站:  ";
page.Canvas.DrawString(text, font1, PdfBrushes.Black, newPointF(x, y));
 
PdfStringFormat format = new PdfStringFormat();
format.MeasureTrailingSpaces = true;           
x = x + font1.MeasureString(text, format).Width;
 
//创建字体
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Underline), true);
//创建PdfTextWebLink对象
PdfTextWebLink webLink = new PdfTextWebLink();
//设置超链接地址
webLink.Url = "https://www.e-iceblue.cn/";
//设置超链接文本
webLink.Text = "www.e-iceblue.cn";
//设置超链接字体和字体颜色
webLink.Font = font2;
webLink.Brush = PdfBrushes.Blue;
 
//添加超链接到页面
webLink.DrawTextWebLink(page.Canvas, new PointF(x, y));
 
//保存文档
pdf.SaveToFile("WebLink.pdf");

2.链接到文档内部的指定页面

//创建PDF文档并添加两页
PdfDocument pdf = new PdfDocument();
PdfPageBase page1 =pdf.Pages.Add();
PdfPageBase page2 =pdf.Pages.Add();
 
//创建字体
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true);
 
//添加文本到页面
page1.Canvas.DrawString("第一页",font, PdfBrushes.Black, new PointF(20,20));
page2.Canvas.DrawString("第二页",font, PdfBrushes.Black, new PointF(20,20));
 
string text = "跳转到第二页"; 
//创建RectangleF对象并添加文本         
RectangleF rectangle = new RectangleF(20,80, 70, 20);
page1.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rectangle);
 
//创建PdfDocumentLinkAnnotation对象
PdfDocumentLinkAnnotation documentLink = new PdfDocumentLinkAnnotation(rectangle,new PdfDestination(page2));
 
//设置边框颜色           
documentLink.Color = Color.DarkSeaGreen;
 
//添加超链接到第一页
page1.AnnotationsWidget.Add(documentLink);
 
//保存文档
pdf.SaveToFile("InternalFileLink.pdf");

3.链接到外部文档

//创建PDF文档并添加一页
PdfDocument document = new PdfDocument();
PdfPageBase page =document.Pages.Add();
 
//创建字体
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true);
 
string text = "打开1.jpg";
//创建RectangleF对象并添加文本
RectangleF rectangle = new RectangleF(20,40, 80, 20);
page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rectangle);
           
//创建PdfFileLinkAnnotation对象
PdfFileLinkAnnotation fileLink = new PdfFileLinkAnnotation(rectangle,@"1.jpg");
//设置超链接边框颜色
fileLink.Color = Color.DarkSeaGreen;
 
//添加超链接到页面
page.AnnotationsWidget.Add(fileLink);
 
//保存文档
document.SaveToFile("ExternalFileLink.pdf");

到此这篇关于C#代码实现将超链接插入到PDF的现有文本中的文章就介绍到这了,更多相关C#超链接插入PDF内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文