This example demonstrates how to add web links, e-mail links, file links, and bookmarks to a document.
A hyperlink is a reference to data that the reader can directly follow either by clicking. A hyperlink points to a specific element within a document. Hypertext is text with hyperlinks.
There are following types of hyperlinks that we can used in our documents.
- Web Link
- Email Link
- Image Link
- File Link
- Bookmark Link
Web link is used for linking web address with and without protocols. On clicking the link it navigates to web browser with specified URL.
Email Link has reciever email address. It is used for opening email composer with reciever email. It open system default email software like Outlook etc.
Image Link is used for display image from specified URL
File Link is used for open the specified file
Bookmark link is used to navigate to the specified content of the document which is marked for ease.
We have versions of this representation in words ,using dropdown you can make a choice within the following
- Word Doc
- Word Docx
- Word ML
- RTF
- MS Text format
- E-book format
- DLS based xml file format
- HTML
After clicking a generate button will launch a file with above choosen version of word with sample links.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class HyperlinkExample : WordExampleBase
{
public HyperlinkExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public HyperlinkExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument();
// Add a new section to the document.
Section section = document.AddSection();
Paragraph para = section.AddParagraph();
para.AppendText("Hyperlinks");
para.ApplyStyle(StyleIdentifier.Title);
para = section.AddParagraph();
para.AppendText("Web Hyperlinks");
para.ApplyStyle(StyleIdentifier.Heading2);
// Web Hyperlink.
para = section.AddParagraph();
Field field = para.AppendField("OfficeComponent", FieldType.FieldHyperlink);
para.ApplyStyle(StyleIdentifier.Hyperlink);
OfficeComponent.Word.Hyperlink link = new OfficeComponent.Word.Hyperlink(field);
link.Type = HyperlinkType.WebLink;
link.Uri = "http://www.officecomponent.com";
para = section.AddParagraph();
para.AppendText("Email Hyperlinks");
para.ApplyStyle(StyleIdentifier.Heading2);
// Email Hyperlink
para = section.AddParagraph();
field = para.AppendField("OfficeComponent Sales Team", FieldType.FieldHyperlink);
para.ApplyStyle(StyleIdentifier.Hyperlink);
link = new OfficeComponent.Word.Hyperlink(field);
link.Type = HyperlinkType.EMailLink;
link.Uri = "mailto:sales@officecomponent.com";
para = section.AddParagraph();
para.AppendText("Image Hyperlinks");
para.ApplyStyle(StyleIdentifier.Heading2);
// Image Hyperlink
para = section.AddParagraph();
OfficeComponent.Word.Picture picture = new Picture(document);
picture.LoadImage(System.Drawing.Image.FromFile(CommonDataPath + "\\Logo.gif"));
field = para.AppendField("ImageLink", FieldType.FieldHyperlink);
para.ApplyStyle(StyleIdentifier.Hyperlink);
link = new OfficeComponent.Word.Hyperlink(field);
link.Type = HyperlinkType.WebLink;
link.Uri = "http://www.officecomponent.com/";
link.PictureToDisplay = picture;
para = section.AddParagraph();
para.AppendText("File Hyperlinks");
para.ApplyStyle(StyleIdentifier.Heading2);
// File Hyperlink
para = section.AddParagraph();
field = para.AppendField("FileLink", FieldType.FieldHyperlink);
para.ApplyStyle(StyleIdentifier.Hyperlink);
link = new OfficeComponent.Word.Hyperlink(field);
link.Type = HyperlinkType.FileLink;
link.FilePath = CommonDataPath + "\\EULA.txt";
para = section.AddParagraph();
para.AppendText("Bookmark Hyperlinks");
para.ApplyStyle(StyleIdentifier.Heading2);
para = section.AddParagraph();
para.AppendBookmarkStart("AboutUs");
para.AppendText("About OfficeComponent");
para.ApplyStyle(StyleIdentifier.Heading3);
para.AppendBookmarkEnd("AboutUs");
string content;
using (StreamReader reader = new StreamReader(CommonDataPath + "\\About.txt"))
{
content = reader.ReadToEnd();
}
para.AppendText(content);
para = section.AddParagraph();
para.AppendBookmarkStart("OfficeComponentWord");
para.AppendText("OfficeComponent Word for .NET");
para.ApplyStyle(StyleIdentifier.Heading3);
para.AppendBookmarkEnd("OfficeComponentWord");
using (StreamReader reader = new StreamReader(CommonDataPath + "\\OfficeComponentWord.txt"))
{
content = reader.ReadToEnd();
}
para.AppendText(content);
// Bookmark Hyperlink
para = section.AddParagraph();
field = para.AppendField("About OfficeComponent", FieldType.FieldHyperlink);
para.ApplyStyle(StyleIdentifier.Hyperlink);
link = new OfficeComponent.Word.Hyperlink(field);
link.Type = HyperlinkType.Bookmark;
link.BookmarkName = "AboutUs";
// Bookmark Hyperlink
para = section.AddParagraph();
field = para.AppendField("OfficeComponentWord Overview", FieldType.FieldHyperlink);
para.ApplyStyle(StyleIdentifier.Hyperlink);
link = new OfficeComponent.Word.Hyperlink(field);
link.Type = HyperlinkType.Bookmark;
link.BookmarkName = "OfficeComponentWord";
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
Friend Class HyperlinkExample
Inherits WordExampleBase
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim document As New WordDocument()
' Add a new section to the document.
Dim section As Section = document.AddSection()
Dim para As Paragraph = section.AddParagraph()
para.AppendText("Hyperlinks")
para.ApplyStyle(StyleIdentifier.Title)
para = section.AddParagraph()
para.AppendText("Web Hyperlinks")
para.ApplyStyle(StyleIdentifier.Heading2)
' Web Hyperlink.
para = section.AddParagraph()
Dim field As Field = para.AppendField("OfficeComponent", FieldType.FieldHyperlink)
para.ApplyStyle(StyleIdentifier.Hyperlink)
Dim link As New OfficeComponent.Word.Hyperlink(field)
link.Type = HyperlinkType.WebLink
link.Uri = "http://www.officecomponent.com"
para = section.AddParagraph()
para.AppendText("Email Hyperlinks")
para.ApplyStyle(StyleIdentifier.Heading2)
' Email Hyperlink
para = section.AddParagraph()
field = para.AppendField("OfficeComponent Sales Team", FieldType.FieldHyperlink)
para.ApplyStyle(StyleIdentifier.Hyperlink)
link = New OfficeComponent.Word.Hyperlink(field)
link.Type = HyperlinkType.EMailLink
link.Uri = "mailto:sales@officecomponent.com"
para = section.AddParagraph()
para.AppendText("Image Hyperlinks")
para.ApplyStyle(StyleIdentifier.Heading2)
' Image Hyperlink
para = section.AddParagraph()
Dim picture As OfficeComponent.Word.Picture = New Picture(document)
picture.LoadImage(System.Drawing.Image.FromFile(CommonDataPath & "\Logo.gif"))
field = para.AppendField("ImageLink", FieldType.FieldHyperlink)
para.ApplyStyle(StyleIdentifier.Hyperlink)
link = New OfficeComponent.Word.Hyperlink(field)
link.Type = HyperlinkType.WebLink
link.Uri = "http://www.officecomponent.com/"
link.PictureToDisplay = picture
para = section.AddParagraph()
para.AppendText("File Hyperlinks")
para.ApplyStyle(StyleIdentifier.Heading2)
' File Hyperlink
para = section.AddParagraph()
field = para.AppendField("FileLink", FieldType.FieldHyperlink)
para.ApplyStyle(StyleIdentifier.Hyperlink)
link = New OfficeComponent.Word.Hyperlink(field)
link.Type = HyperlinkType.FileLink
link.FilePath = CommonDataPath & "\EULA.txt"
para = section.AddParagraph()
para.AppendText("Bookmark Hyperlinks")
para.ApplyStyle(StyleIdentifier.Heading2)
para = section.AddParagraph()
para.AppendBookmarkStart("AboutUs")
para.AppendText("About OfficeComponent")
para.ApplyStyle(StyleIdentifier.Heading3)
para.AppendBookmarkEnd("AboutUs")
Dim content As String
Using reader As New StreamReader(CommonDataPath & "\About.txt")
content = reader.ReadToEnd()
End Using
para.AppendText(content)
para = section.AddParagraph()
para.AppendBookmarkStart("OfficeComponentWord")
para.AppendText("OfficeComponent Word for .NET")
para.ApplyStyle(StyleIdentifier.Heading3)
para.AppendBookmarkEnd("OfficeComponentWord")
Using reader As New StreamReader(CommonDataPath & "\OfficeComponentWord.txt")
content = reader.ReadToEnd()
End Using
para.AppendText(content)
' Bookmark Hyperlink
para = section.AddParagraph()
field = para.AppendField("About OfficeComponent", FieldType.FieldHyperlink)
para.ApplyStyle(StyleIdentifier.Hyperlink)
link = New OfficeComponent.Word.Hyperlink(field)
link.Type = HyperlinkType.Bookmark
link.BookmarkName = "AboutUs"
' Bookmark Hyperlink
para = section.AddParagraph()
field = para.AppendField("OfficeComponentWord Overview", FieldType.FieldHyperlink)
para.ApplyStyle(StyleIdentifier.Hyperlink)
link = New OfficeComponent.Word.Hyperlink(field)
link.Type = HyperlinkType.Bookmark
link.BookmarkName = "OfficeComponentWord"
Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName, SaveAsFormat)
' Close the document.
document.Close()
Return fileName
End Function
End Class
End Namespace