C# 插入超連接到PDF文檔(3種狀況)

超連接能夠實現不一樣元素之間的鏈接,用戶能夠經過點擊被連接的元素來激活這些連接。具備高效、快捷、準確的特色。本文中,將分享經過C#編程在PDF文檔中插入超連接的方法。內容包含如下要點:html

  • 插入網頁連接
  • 插入外部文檔連接
  • 插入文檔頁面跳轉連接

工具web

下載安裝後,注意將Spire.Pdf.dll引用到程序(dll文件可在安裝路徑下的Bin文件夾中獲取)編程

示例代碼(供參考)ide

【示例1】插入網頁連接工具

步驟 1:建立實例,並添加頁字體

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

步驟 2:定義座標變量spa

float x = 10;
float y = 50;

步驟 3:建立字體1,並添加文本到頁面3d

//建立字體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 ,添加超連接文本,並設置格式code

 //建立字體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 :添加超連接到頁面,並保存文檔orm

//添加超連接到頁面
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

(本文完)

轉載請註明出處。

相關文章
相關標籤/搜索