当前位置: 移动技术网 > IT编程>开发语言>.net > C# 插入超链接到PDF文档(3种情况)

C# 插入超链接到PDF文档(3种情况)

2018年10月25日  | 移动技术网IT编程  | 我要评论

沧州招聘,97ndd,深圳人流手术多少钱

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

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

工具

下载安装后,注意将spire.pdf.dll引用到程序(dll文件可在安装路径下的bin文件夹中获取)

示例代码(供参考)

【示例1】插入网页链接

步骤 1:创建实例,并添加页

pdfdocument pdf = new pdfdocument();
pdfpagebase page = pdf.pages.add();

步骤 2:定义坐标变量

float x = 10;
float y = 50;

步骤 3:创建字体1,并添加文本到页面

//创建字体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;

步骤 4:创建字体2 ,添加超链接文本,并设置格式

 //创建字体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;

步骤 5 :添加超链接到页面,并保存文档

//添加超链接到页面
weblink.drawtextweblink(page.canvas, new pointf(x, y+15));

//保存文档
pdf.savetofile("weblink.pdf");

网页链接效果:

全部代码:

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;

            //创建字体
            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;

            //创建字体
            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");
        }
    }
}
view code

 

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

 步骤 1:创建实例,并添加页

pdfdocument document = new pdfdocument();
pdfpagebase page = document.pages.add();

步骤 2:创建字体,并绘制超链接文本

//创建字体
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;

步骤 3 :添加超链接到页面,并保存文档

//添加超链接到页面
page.annotationswidget.add(filelink);

//保存并打开文档
document.savetofile("externalfilelink.pdf");

外部文档连接效果:

全部代码:

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");
        }
    }
}
view code

 

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

步骤 1 :创建文档,并添加3页

pdfdocument pdf = new pdfdocument();
pdfpagebase page1 = pdf.pages.add();
pdfpagebase page2 = pdf.pages.add();
pdfpagebase page3 = pdf.pages.add();

步骤 2:创建字体,添加文本到页面

//创建字体
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;

步骤 3: 添加超链接到页面并保存文档

//添加超链接到第一页
page1.annotationswidget.add(documentlink);

//保存文档
pdf.savetofile("internalfilelink.pdf");

页面跳转链接效果:

全部代码:

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");
        }
    }
}
view code

(本文完)

转载请注明出处。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网