Hyperlink

Hyperlink (4)

Friday, 27 June 2014 08:50

How to Modify Hyperlinks in Word Document

Written by support iceblue

Hyperlinks have two basic parts: the address (URL) of a certain object and the display text. As an essential element in electronic document, hyperlinks are frequently used in Word to redirect to large amount of information without occupying much space. However, Word doesn't always get the hyperlink correct. In this article, I'll introduce you how to change the displayed text of a hyperlink in C# via Spire.Doc for .NET.

Here is a Word document that makes a brief introduction about Spire.Doc, but as you can see from the picture below, the first hyperlink has been embedded with a wrong text description.

Modify Hyperlinks in Word Document

Now, we are able to change the hypertext as "Spire.Doc component" by using the following method.

Step 1: Create a new instance of Word document and load the sample file.

Document document = new Document();
document.LoadFromFile("..\\..\\Spire.Doc.docx");

Step 2: Call FindAllHyperlinks() method to find all hyperlinks in the Word document.

static List FindAllHyperlinks(Document document)
        {
            List hyperlinks = new List();
            foreach (Section section in document.Sections)
            {
                foreach (DocumentObject sec in section.Body.ChildObjects)
                {
                    if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                        {
                            if (para.DocumentObjectType == DocumentObjectType.Field)
                            {
                                Field field = para as Field;

                                if (field.Type == FieldType.FieldHyperlink)
                                {
                                    hyperlinks.Add(field);
                                }
                            }
                        }
                    }
                }
            }

Step 3: Reset the property of hyperlinks[0].FieldText by using the index of the hyperlink.

hyperlinks[0].FieldText = "Spire.Doc component";

string outfile="result.doc";
document.SaveToFile(outfile,FileFormat.Doc);
System.Diagnostics.Process.Start(outfile);

Result:

Modify Hyperlinks in Word Document

Thursday, 26 June 2014 06:42

Finding Hyperlinks in a word document in C#

Written by support iceblue

Hyperlinks can point to files, emails, websites, imagers or video when readers click on it. Hyperlinks are widely used in word document for it is very convenient to direct readers to related, useful content. By using Spire.Doc, developers can add hyperlinks, finding hyperlinks and modify hyperlinks. This article will show you how to find all the existing hyperlinks in a word document in C#.

Download and install Spire.Doc for .NET and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to finding hyperlinks in C#.

Firstly, view the word document which contains many hyperlinks:

Finding Hyperlinks in a word document

Please check the code of how to find all the hyperlinks in word document:

static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Spire.docx");
            List hyperlinks = new List();
            foreach (Section section in doc.Sections)
            {
                foreach (DocumentObject sec in section.Body.ChildObjects)
                {
                    if (sec.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject para in (sec as Paragraph).ChildObjects)
                        {
                            if (para.DocumentObjectType == DocumentObjectType.Field)
                            {
                                Field field = para as Field;

                                if (field.Type == FieldType.FieldHyperlink)
                                {
                                    hyperlinks.Add(field);
                                }
                            }
                        }
                    }
                }
            }

The effective screenshot of the finding hyperlinks:

Finding Hyperlinks in a word document

Tuesday, 11 September 2012 02:26

Insert Word Hyperlink in Silverlight

Written by support iceblue

Word hyperlink can guide users to get other related information to contents. It can be Email address, website link or even other documents. When inserting hyperlink, users can add for specified text or enter link address directly.

Spire.Doc for Silverlight, a powerful component on manipulating Word documents with Silverlight, enables users to insert hyperlink in Word with Silverlight. This guide will show the method to realize inserting word hyperlinks in Silverlight. Users can invoke paragraph.AppendHyperlink(link string, text string, hyperlink type) method which is provided by Spire.Doc for Silverlight directly to insert hyperlink in Word.

Download Spire.Doc for Silverlight. Create a Silverlight application. Add a button in MainPage.xaml, and then double it to use the following code to insert hyperlink.

The following screenshot demonstrates the result after inserting hyperlink to Word in Silverlight. The hyperlink is located at the bottom of the picture.

Insert Word Hyperlink

Sample code:

[C#]
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertHyperlink
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Declare SaveFileDialog
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Word Document(*.docx)|*.docx";

            //Load Document
            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith("Spire.Doc for Silverlight.docx"))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

            //Add Hyperlink
            Section section = document.Sections[0];
            Paragraph LinkPara = section.AddParagraph();
            LinkPara.AppendHyperlink("http://www.e-iceblue.com/Introduce/word-for-silverlight-introduce.html", "Learn More about Spire.Doc for Siverlight", HyperlinkType.WebLink);

            //Set Paragrah Style
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = "LinkStyle";
            style.CharacterFormat.FontName = "Britannic Bold";
            style.CharacterFormat.FontSize = 13;
            style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
            style.ParagraphFormat.LineSpacing = 15F;
            document.Styles.Add(style);
            LinkPara.ApplyStyle(style.Name);

            //Save Document
            bool? result = save.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = save.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }  
        }
    }
}
[VB.NET]
Imports System.IO
Imports System.Reflection
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        'Declare SaveFileDialog
        Dim save As New SaveFileDialog()
        save.Filter = "Word Document(*.docx)|*.docx"

        'Load Document
        Dim document As New Document()
        Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
        For Each name As String In [assembly].GetManifestResourceNames()
            If name.EndsWith("Spire.Doc for Silverlight.docx") Then
                Using docStream As Stream = [assembly].GetManifestResourceStream(name)
                    document = New Document(docStream, FileFormat.Docx)
                End Using
            End If
        Next name

        'Add Hyperlink
        Dim section As Section = document.Sections(0)
        Dim LinkPara As Paragraph = section.AddParagraph()
        LinkPara.AppendHyperlink("http://www.e-iceblue.com/Introduce/word-for-silverlight-introduce.html", "Learn More about Spire.Doc for Siverlight", HyperlinkType.WebLink)

        'Set Paragrah Style
        Dim style As New ParagraphStyle(document)
        style.Name = "LinkStyle"
        style.CharacterFormat.FontName = "Britannic Bold"
        style.CharacterFormat.FontSize = 13
        style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple
        style.ParagraphFormat.LineSpacing = 15.0F
        document.Styles.Add(style)
        LinkPara.ApplyStyle(style.Name)

        'Save Document
        Dim result? As Boolean = save.ShowDialog()
        If result.HasValue AndAlso result.Value Then
            Using stream As Stream = save.OpenFile()
                document.SaveToStream(stream, FileFormat.Docx)
            End Using
        End If
    End Sub
End Class

Spire.Doc is an Microsoft Word component which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF.NET and Silverlight.

Friday, 07 January 2011 07:10

Insert Hyperlink to Word in C#, VB.NET

Written by Administrator

Word Hyperlink can be inserted automatically when users press ENTER or SPACEBAR after typing address of an existing Web page, such as www.e-iceblue.com. Also, users can insert customized hyperlink in Word for text to picture that they want to display as hyperlink. The hyperlink for text or picture can be Web page, E-mail address, other files or document.

Spire.Doc for .NET, a professional .NET Word component to manipulate Word documents, enables users to insert hyperlink in Word with C#/VB.NET. You can invoke the method paragraph.AppendHyperlink(string link, string text, HyperlinkType) to insert hyperlink in Word directly via Spire.Doc for .NET. And the HyperlinkType in method can be set as WebLink, EmailLink etc. Below is an effective screenshot after inserting hyperlink to Word in C#, VB.NET

Insert Word Hyperlink

Download and install Spire.Doc for .NET and then use the following code to insert hyperlink in Word.

[C#]
            //Create Word
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Set Paragraph Styles
            ParagraphStyle txtStyle = new ParagraphStyle(document);
            txtStyle.Name = "Style";
            txtStyle.CharacterFormat.FontName = "Impact";
            txtStyle.CharacterFormat.FontSize = 16;
            txtStyle.CharacterFormat.TextColor = Color.RosyBrown;
            document.Styles.Add(txtStyle);

            ParagraphStyle hyperlinkstyle = new ParagraphStyle(document);
            hyperlinkstyle.Name = "linkStyle";
            hyperlinkstyle.CharacterFormat.FontName = "Calibri";
            hyperlinkstyle.CharacterFormat.FontSize = 15;
            document.Styles.Add(hyperlinkstyle);

            //Append Text and Set Text Format
            paragraph = section.AddParagraph();
            paragraph.AppendText("Home page");
            paragraph.ApplyStyle(txtStyle.Name);

            //Insert Hyperlink(Web Page)
            paragraph = section.AddParagraph();
            paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
            paragraph.ApplyStyle(hyperlinkstyle.Name);

            //Append text
            paragraph = section.AddParagraph();
            paragraph.AppendText("Contact US");
            paragraph.ApplyStyle(txtStyle.Name);

            //Insert Hyperlink(E-mail Address);
            paragraph = section.AddParagraph();
            paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);
            paragraph.ApplyStyle(hyperlinkstyle.Name);

            //Save and Launch
            document.SaveToFile("Hyperlink.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Hyperlink.docx");
[VB.NET]
           'Create Word
            Dim document As New Document()
            Dim section As Section = document.AddSection()
            Dim paragraph As Paragraph = section.AddParagraph()

            'Set Paragraph Styles
            Dim txtStyle As New ParagraphStyle(document)
            txtStyle.Name = "Style"
            txtStyle.CharacterFormat.FontName = "Impact"
            txtStyle.CharacterFormat.FontSize = 16
            txtStyle.CharacterFormat.TextColor = Color.RosyBrown
            document.Styles.Add(txtStyle)

            Dim hyperlinkstyle As New ParagraphStyle(document)
            hyperlinkstyle.Name = "linkStyle"
            hyperlinkstyle.CharacterFormat.FontName = "Calibri"
            hyperlinkstyle.CharacterFormat.FontSize = 15
            document.Styles.Add(hyperlinkstyle)

            'Append Text and Set Text Format
            paragraph = section.AddParagraph()
            paragraph.AppendText("Home page")
            paragraph.ApplyStyle(txtStyle.Name)

            'Insert Hyperlink(Web Page)
            paragraph = section.AddParagraph()
            paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink)
            paragraph.ApplyStyle(hyperlinkstyle.Name)

            'Append text
            paragraph = section.AddParagraph()
            paragraph.AppendText("Contact US")
            paragraph.ApplyStyle(txtStyle.Name)

            'Insert Hyperlink(E-mail Address);
            paragraph = section.AddParagraph()
            paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink)
            paragraph.ApplyStyle(hyperlinkstyle.Name)

            'Save and Launch
            document.SaveToFile("Hyperlink.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Hyperlink.docx")

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.