Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
4 juin 2012

With Silverlight-How to Insert Hyperlink In Word

When writing blog post, we may add some hyperlinks to link to other related or recommended articles at the end of post. Also, MS Word provides users with a function to insert hyperlink. In Word, hyperlink can not only link to website which are related to document contents, but other files or Email address.

Why do we insert hyperlink in Word? Sometimes, we want to show some more additional information in one document but the information is not so much clear to main idea of this document. So, we insert a hyperlink to point to the additional information and the contents will not be influenced.

In this post, I want to share my method about how to insert hyperlink in Word with Silverlight. Also, Spire.XLS for Silverlight is used in this example, so I have added its dll file as reference in my project.

STEPS:

Step 1. Design User Control

Rename MainPage.xaml as Hyperlink.xaml and double click it to design UserControl. Firstly, add a label. Change its contents as “Insert Hyperlink in Word with Silverlight” and format contents, including text font style and color. Secondly, add a button to Run. Thirdly, set UserControl background color.

Hyperlink.xaml

<UserControlx:Class="WordHyperlinkInSilverlight.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="178"d:DesignWidth="493"xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"FontWeight="Bold">

 

    <Gridx:Name="LayoutRoot"Height="178"Width="491">

        <sdk:LabelHeight="88"HorizontalAlignment="Center"Margin="12,20,14,77"Name="label1"VerticalAlignment="Center"Width="447"Content="Insert Hyperlink in Word with Silverlight"FontSize="20"FontFamily="Arial Black"FontWeight="Normal"Foreground="White" />

        <ButtonContent="RUN"Height="41"HorizontalAlignment="Left"Margin="421,110,0,0"Name="button1"VerticalAlignment="Top"Width="58"Click="button1_Click" />

        <Grid.Background>

            <LinearGradientBrushEndPoint="1,0.5"StartPoint="0,0.5">

                <GradientStopColor="Black"Offset="0" />

                <GradientStopColor="#FF66E8E8"Offset="1" />

                <GradientStopColor="#FF429898"Offset="0" />

                <GradientStopColor="#FF0F5A5A"Offset="0" />

            </LinearGradientBrush>

        </Grid.Background>

    </Grid>

</UserControl>

Step 2. Declare saveFileDialog

Declare a saveFileDialog for using to save Word document following. Also, set filter for this saveFileDialog, which is about Word document version, doc or docx.

        private SaveFileDialog saveFileDialog = null;

        public MainPage()

        {

            InitializeComponent();

 

            this.saveFileDialog = new SaveFileDialog();

            this.saveFileDialog.Filter = "Word Document(*.docx)|*.docx";

        }

Step 3. Insert Hyperlink

Double click RUN button and write code.

Firstly, declare a new document and then add section and paragraph in this section. Also, define a paragraph style, including font type, size and line space.

            Document document = new Document();

            Section section = document.AddSection();

            Paragraph paragraph1 = section.AddParagraph();

 

            ParagraphStyle weblinkstyle = new ParagraphStyle(document);

            weblinkstyle.Name = "LinkStyle";

            weblinkstyle.CharacterFormat.FontName = "Times New Roman";

            weblinkstyle.CharacterFormat.FontSize = 18;

            weblinkstyle.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;

            weblinkstyle.ParagraphFormat.LineSpacing = 20;

            document.Styles.Add(weblinkstyle);

Secondly, insert two kinds of hyperlinks in different paragraphs. Use paragraph1/2.AppendHyperlink() method to insert hyperlinks. Three parameters are passed to this method, link string, text string and hyperlink type. For first paragraph, the hyperlink type is WebLink and second is EMailLink. Then, apply paragraph style for the two paragraphs by using paragraph1/2.ApplyStyle() method.

            paragraph1.AppendHyperlink("http://en.wikipedia.org/wiki/Wiki", "Wikipedia, the free encyclopedia", HyperlinkType.WebLink);

            paragraph1.ApplyStyle(weblinkstyle.Name);

 

            Spire.Doc.Documents.Paragraph paragraph2 = section.AddParagraph();

            paragraph2.AppendHyperlink("mailto:author.me@gmail.com", "Send Mail to Me", HyperlinkType.EMailLink);

            paragraph2.ApplyStyle(weblinkstyle.Name);

Step 4. Save File

Judge if the saveFiledialog which has been declared can pop up. If yes, save the Word document by using save file dialog box.

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream saveStr = this.saveFileDialog.OpenFile())

                {

                    document.SaveToStream(saveStr, FileFormat.Docx);

                }

            }

Full Hyperlink.xaml.cs

using System.Windows;

using System.Windows.Controls;

using System.IO;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace WordHyperlinkInSilverlight

{

    public partial class MainPage : UserControl

    {

        private SaveFileDialog saveFileDialog = null;

        public MainPage()

        {

            InitializeComponent();

 

            this.saveFileDialog = new SaveFileDialog();

            this.saveFileDialog.Filter = "Word Document(*.docx)|*.docx";

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            Document document = new Document();

            Section section = document.AddSection();

            Paragraph paragraph1 = section.AddParagraph();

 

            ParagraphStyle weblinkstyle = new ParagraphStyle(document);

            weblinkstyle.Name = "LinkStyle";

            weblinkstyle.CharacterFormat.FontName = "Times New Roman";

            weblinkstyle.CharacterFormat.FontSize = 18;

            weblinkstyle.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;

            weblinkstyle.ParagraphFormat.LineSpacing = 20;

            document.Styles.Add(weblinkstyle);

 

            paragraph1.AppendHyperlink("http://en.wikipedia.org/wiki/Wiki", "Wikipedia, the free encyclopedia", HyperlinkType.WebLink);

            paragraph1.ApplyStyle(weblinkstyle.Name);

 

            Spire.Doc.Documents.Paragraph paragraph2 = section.AddParagraph();

            paragraph2.AppendHyperlink("mailto:author.me@gmail.com", "Send Mail to Me", HyperlinkType.EMailLink);

            paragraph2.ApplyStyle(weblinkstyle.Name);

 

            bool? result = this.saveFileDialog.ShowDialog();

            if (result.HasValue && result.Value)

            {

                using (Stream saveStr = this.saveFileDialog.OpenFile())

                {

                    document.SaveToStream(saveStr, FileFormat.Docx);

                }

            }

        }

    }

}

RESULT

__________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Click Here to DOWNLOAD Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité