温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# 添加超链接到PDF文档

发布时间:2020-07-30 04:44:43 来源:网络 阅读:273 作者:E_iceblue 栏目:编程语言

概述

超链接可以实现不同元素之间的连接,用户可以通过点击被链接的元素来激活这些链接。具有高效、快捷、准确的特点。本文中,将分享通过C#编程在PDF文档中插入超链接的方法。内容包含以下要点:

  • 插入网页链接
  • 插入外部文档链接
  • 插入文档页面跳转链接

工具

  • Free Spire.PDF for .NET (免费版)

下载安装后,注意将Spire.Pdf.dll引用到程序(dll文件可在安装路径下的Bin文件夹中获取)
C# 添加超链接到PDF文档

示例代码(供参考)

【示例1】插入网页链接

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace Weblink
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PDF文档并添加一页
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //定义坐标变量并赋初值
            float x = 10;
            float y = 50;

            //创建字体1
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true); 
            //添加文本到页面
            string text = "注:\n本文主要数据来源参考自WTO,查看原文请点击:";
            page.Canvas.DrawString(text, font1, PdfBrushes.Black, new PointF(x, y));
            PdfStringFormat format = new PdfStringFormat();
            format.MeasureTrailingSpaces = true;
            x = x + font1.MeasureString(text, format).Width;

            //创建字体2
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Underline), true);
            //创建PdfTextWebLink对象
            PdfTextWebLink webLink = new PdfTextWebLink();
            //设置超链接地址
            webLink.Url = "https://www.wto.org/";
            //设置超链接文本
            webLink.Text = "WTO Official Website";
            //设置超链接字体和字体颜色
            webLink.Font = font2;
            webLink.Brush = PdfBrushes.Blue;

            //添加超链接到页面
            webLink.DrawTextWebLink(page.Canvas, new PointF(x, y+15));

            //保存文档
            pdf.SaveToFile("WebLink.pdf");
            System.Diagnostics.Process.Start("Weblink.pdf");
        }
    }
}

网页链接效果:
C# 添加超链接到PDF文档

【示例2】链接到外部文档

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace Filelink
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PDF文档并添加一页
            PdfDocument document = new PdfDocument();
            PdfPageBase page = document.Pages.Add();

            //创建字体
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 15f, FontStyle.Regular), true);

            string text = "Clik and View the Original Document";
            //创建RectangleF对象并添加文本
            RectangleF rectangle = new RectangleF(20, 40, 300,40);
            page.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle);

            //创建PdfFileLinkAnnotation对象 
            PdfFileLinkAnnotation fileLink = new PdfFileLinkAnnotation(rectangle, @"sample.docx");
            //设置超链接边框颜色
            fileLink.Color = Color.White;

            //添加超链接到页面
            page.AnnotationsWidget.Add(fileLink);

            //保存并打开文档
            document.SaveToFile("ExternalFileLink.pdf");
            System.Diagnostics.Process.Start("ExternalFileLink.pdf");
        }
    }
}

外部文档连接效果:
C# 添加超链接到PDF文档

【示例3】插入文档页面跳转链接

using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace Documentlink
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建PDF文档并添加3页
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page1 = pdf.Pages.Add();
            PdfPageBase page2 = pdf.Pages.Add();
            PdfPageBase page3 = 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));
            page3.Canvas.DrawString("(第三页)", font, PdfBrushes.Black, new PointF(20, 20));

            //创建超链接文本
            string text = "点击跳转至最后一页";

            //创建RectangleF对象并添加文本          
            RectangleF rectangle = new RectangleF(40, 50, 900, 20);
            page1.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle);

            //创建PdfDocumentLinkAnnotation对象
            PdfDocumentLinkAnnotation documentLink = new PdfDocumentLinkAnnotation(rectangle, new PdfDestination(page3));

            //设置边框颜色            
            documentLink.Color = Color.White;

            //添加超链接到第一页
            page1.AnnotationsWidget.Add(documentLink);

            //保存文档
            pdf.SaveToFile("InternalFileLink.pdf");
            System.Diagnostics.Process.Start("InternalFileLink.pdf");
        }
    }
}

页面跳转链接效果:
C# 添加超链接到PDF文档

(本文完)
转载请注明出处。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI