Link has already becomes a quickest way for people to reach one webpage from another. When people search something on internet, they only need to insert the keywords in search engine, and click the right link. They can find what they want. But link function is used far from this. Link also can be inserted to different documents of various formats. PDF is just one. When we click the link in the documents, we also can reach another webpage. Link is really marvelous. Now, let us see how to add link in PDF Document with C#,VB.NET.
In order to realize my task smoothly, I choose a PDF document creation component Spire.PDF for .NET to help me, you can:
Freely download Spire.PDF
In order to realize my task smoothly, I choose a PDF document creation component Spire.PDF for .NET to help me, you can:
Freely download Spire.PDF
Procedure
Step1. Create a new project.
1.Create a new project in Console Application.
2.Set thetarget Framework to be the .NET Framework 4.
Step2. Add reference.
1.Add Spire.PDF Dll and System Drawing as references.
2.Add the following using to the top of the method.
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
Step3. Insert link in PDF Document.
1.Create a PDF Document and set its margin.
C# Code:
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
1.Create a PDF Document and set its margin.
C# Code:
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
VB.NET Code:
'Create a pdf document.
Dim doc As New PdfDocument()
'margin
Dim unitCvtr As New PdfUnitConvertor()
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left
'Create a pdf document.
Dim doc As New PdfDocument()
'margin
Dim unitCvtr As New PdfUnitConvertor()
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left
2.Create one page and add links in the page .
C# Code:
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
float y = 10;
float x = 0;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12));
String label = "Simple Link: ";
PdfStringFormat format = new PdfStringFormat();
format.MeasureTrailingSpaces = true;
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12, FontStyle.Underline));
String url1 = http://www.e-iceblue.com;
page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y);
y = y + font1.MeasureString(url1).Height;
label = "Web Link: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
String text = "e-iceblue";
PdfTextWebLink link2 = new PdfTextWebLink();
link2.Text = text;
link2.Url = url1;
link2.Font = font1;
link2.Brush = PdfBrushes.Blue;
link2.DrawTextWebLink(page.Canvas, new PointF(x, y));
y = y + font1.MeasureString(text).Height;
label = "URI Annonationa: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
text = "Google";
PointF location = new PointF(x, y);
SizeF size = font1.MeasureString(text);
RectangleF linkBounds = new RectangleF(location, size);
PdfUriAnnotation link3 = new PdfUriAnnotation(linkBounds);
link3.Border = new PdfAnnotationBorder(0);
link3.Uri = http://www.google.com;
(page as PdfNewPage).Annotations.Add(link3);
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
y = y + size.Height;
label = "URI Annonationa Action: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
text = "JavaScript Action (Click Me)";
location = new PointF(x, y);
size = font1.MeasureString(text);
linkBounds = new RectangleF(location, size);
PdfUriAnnotation link4 = new PdfUriAnnotation(linkBounds);
link4.Border = new PdfAnnotationBorder(0.75f);
link4.Color = Color.LightGray;
//script
String script
= "app.alert({"
+ " cMsg: \"Hello.\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action = new PdfJavaScriptAction(script);
link4.Action = action;
(page as PdfNewPage).Annotations.Add(link4);
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
y = y + size.Height;
C# Code:
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
float y = 10;
float x = 0;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12));
String label = "Simple Link: ";
PdfStringFormat format = new PdfStringFormat();
format.MeasureTrailingSpaces = true;
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12, FontStyle.Underline));
String url1 = http://www.e-iceblue.com;
page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y);
y = y + font1.MeasureString(url1).Height;
label = "Web Link: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
String text = "e-iceblue";
PdfTextWebLink link2 = new PdfTextWebLink();
link2.Text = text;
link2.Url = url1;
link2.Font = font1;
link2.Brush = PdfBrushes.Blue;
link2.DrawTextWebLink(page.Canvas, new PointF(x, y));
y = y + font1.MeasureString(text).Height;
label = "URI Annonationa: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
text = "Google";
PointF location = new PointF(x, y);
SizeF size = font1.MeasureString(text);
RectangleF linkBounds = new RectangleF(location, size);
PdfUriAnnotation link3 = new PdfUriAnnotation(linkBounds);
link3.Border = new PdfAnnotationBorder(0);
link3.Uri = http://www.google.com;
(page as PdfNewPage).Annotations.Add(link3);
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
y = y + size.Height;
label = "URI Annonationa Action: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
text = "JavaScript Action (Click Me)";
location = new PointF(x, y);
size = font1.MeasureString(text);
linkBounds = new RectangleF(location, size);
PdfUriAnnotation link4 = new PdfUriAnnotation(linkBounds);
link4.Border = new PdfAnnotationBorder(0.75f);
link4.Color = Color.LightGray;
//script
String script
= "app.alert({"
+ " cMsg: \"Hello.\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action = new PdfJavaScriptAction(script);
link4.Action = action;
(page as PdfNewPage).Annotations.Add(link4);
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
y = y + size.Height;
VB.NET Code:
' Create one page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)
Dim y As Single = 10
Dim x As Single = 0
Dim font As New PdfTrueTypeFont(New Font("Arial", 12))
Dim label As String = "Simple Link: "
Dim format As New PdfStringFormat()
format.MeasureTrailingSpaces = True
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 12, FontStyle.Underline))
Dim url1 As String = http://www.e-iceblue.com
page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y)
y = y + font1.MeasureString(url1).Height
label = "Web Link: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
Dim text As String = "e-iceblue"
Dim link2 As New PdfTextWebLink()
link2.Text = text
link2.Url = url1
link2.Font = font1
link2.Brush = PdfBrushes.Blue
link2.DrawTextWebLink(page.Canvas, New PointF(x, y))
y = y + font1.MeasureString(text).Height
label = "URI Annonationa: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
text = "Google"
Dim location As New PointF(x, y)
Dim size As SizeF = font1.MeasureString(text)
Dim linkBounds As New RectangleF(location, size)
Dim link3 As New PdfUriAnnotation(linkBounds)
link3.Border = New PdfAnnotationBorder(0)
link3.Uri = http://www.google.com
TryCast(page, PdfNewPage).Annotations.Add(link3)
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
y = y + size.Height
label = "URI Annonationa Action: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
text = "JavaScript Action (Click Me)"
location = New PointF(x, y)
size = font1.MeasureString(text)
linkBounds = New RectangleF(location, size)
Dim link4 As New PdfUriAnnotation(linkBounds)
link4.Border = New PdfAnnotationBorder(0.75F)
link4.Color = Color.LightGray
'script
Dim script As String _
= "app.alert({" _
& " cMsg: ""Hello.""," _
& " nIcon: 3," _
& " cTitle: ""JavaScript Action""" _
& "});"
Dim action As New PdfJavaScriptAction(script)
link4.Action = action
TryCast(page, PdfNewPage).Annotations.Add(link4)
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
y = y + size.Height
' Create one page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)
Dim y As Single = 10
Dim x As Single = 0
Dim font As New PdfTrueTypeFont(New Font("Arial", 12))
Dim label As String = "Simple Link: "
Dim format As New PdfStringFormat()
format.MeasureTrailingSpaces = True
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 12, FontStyle.Underline))
Dim url1 As String = http://www.e-iceblue.com
page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y)
y = y + font1.MeasureString(url1).Height
label = "Web Link: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
Dim text As String = "e-iceblue"
Dim link2 As New PdfTextWebLink()
link2.Text = text
link2.Url = url1
link2.Font = font1
link2.Brush = PdfBrushes.Blue
link2.DrawTextWebLink(page.Canvas, New PointF(x, y))
y = y + font1.MeasureString(text).Height
label = "URI Annonationa: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
text = "Google"
Dim location As New PointF(x, y)
Dim size As SizeF = font1.MeasureString(text)
Dim linkBounds As New RectangleF(location, size)
Dim link3 As New PdfUriAnnotation(linkBounds)
link3.Border = New PdfAnnotationBorder(0)
link3.Uri = http://www.google.com
TryCast(page, PdfNewPage).Annotations.Add(link3)
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
y = y + size.Height
label = "URI Annonationa Action: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
text = "JavaScript Action (Click Me)"
location = New PointF(x, y)
size = font1.MeasureString(text)
linkBounds = New RectangleF(location, size)
Dim link4 As New PdfUriAnnotation(linkBounds)
link4.Border = New PdfAnnotationBorder(0.75F)
link4.Color = Color.LightGray
'script
Dim script As String _
= "app.alert({" _
& " cMsg: ""Hello.""," _
& " nIcon: 3," _
& " cTitle: ""JavaScript Action""" _
& "});"
Dim action As New PdfJavaScriptAction(script)
link4.Action = action
TryCast(page, PdfNewPage).Annotations.Add(link4)
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
y = y + size.Height
Step4. Save the PDF file and preview
C# Code:
//Save pdf file.
doc.SaveToFile("Link.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Link.pdf");
VB.NET Code:
'Save pdf file.
doc.SaveToFile("Link.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("Link.pdf")