HyperlinkFields.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Drawing
Imports GrapeCity.Documents.Word


'' This sample shows how to add hyperlinks to a
'' Word document using HYPERLNK fileds.
'' It adds some external hyperlinks to Web sites,
'' And also hyperlinks to bookmarks within the document.
'' This sample Is very similar to Hyperlinks,
'' but uses fields rather than hyperlink collections.
Public Class HyperlinkFields
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()
        Dim pars = doc.Body.Paragraphs

        '' 0. Paragraph with an external hyperlink:
        Dim p = pars.Add(
                "Among other things, fields allow to insert hyperlinks into documents. " +
                "Following is a hyperlink to a web address. ")
        '' Append a hyperlink field to it (note that unless we specifically style it, 
        '' the hyperlink will look just like other text):
        Dim hl0 = p.GetRange().SimpleFields.Add(
                "HYPERLINK ""http://www.google.com"" \t ""_blank""",
                "Click to go to www.google.com.")

        '' 1. Paragraph with an external hyperlink with its own style:
        p = pars.Add("Next is another hyperlink, this time with its own style and a custom tooltip. ")
        Dim hl1 = p.GetRange().SimpleFields.Add(
                "HYPERLINK ""https://www.grapecity.com/en/"" \o ""Click to open GrapeCity web page"" \t ""_blank""",
                "Click to go to www.grapecity.com.")

        '' Add a style to the 2nd hyperlink:
        Dim s1 = doc.Styles.Add("Hyperlink style 1", StyleType.Character)
        s1.Font.Color.RGB = Color.Blue
        s1.Font.Size += 2
        s1.Font.Bold = True
        For Each run In hl1.GetRange().Runs
            run.Style = s1
        Next

        '' 2. Link to a bookmark within the document:
        '' We add bookmarks at the top and bottom of the document,
        '' and set up links to jump between them.
        Dim bmkTop = "BookmarkTop"
        Dim bmkBot = "BookmarkEnd"
        p = pars.Add(
                "Hyperlinks can also point to locations within the document. " +
                "We add some filler paragraphs below, followed by a paragraph " +
                $"with a bookmark named '{bmkBot}' attached to it. " +
                "The next hyperlink jumps to that bookmark. ")
        '' Attach a bookmark to this paragraph so we can jump back here:
        p.GetRange().Bookmarks.Add(bmkTop)
        '' A hyperlink to a bookmark:
        Dim hl2 = p.GetRange().SimpleFields.Add(
                $"HYPERLINK \l ""{bmkBot}"" \o ""Jumo to {bmkBot}""",
                $"Click to jump to {bmkBot} at the end of the document.")
        '' Add a style for the 2nd hyperlink:
        Dim s2 = doc.Styles.Add("Hyperlink style 2", StyleType.Character)
        s2.Font.Color.RGB = Color.DarkOrange
        s2.Font.Size += 1
        s2.Font.Italic = True
        For Each run In hl2.GetRange().Runs
            run.Style = s2
        Next

        '' Add filler, bookmarked paragraph after it, and
        '' a link to jump back:
        For i = 0 To 99
            pars.Add($"Filler paragraph {i}.")
        Next
        Dim pb = pars.Add($"{bmkBot} points here. ")
        pb.GetRange().Bookmarks.Add(bmkBot)
        Dim hl3 = pb.GetRange().SimpleFields.Add(
                $"HYPERLINK \l ""{bmkTop}"" \o ""Jumo to {bmkTop}""",
                $"Jump back to {bmkTop}.")
        For Each run In hl3.GetRange().Runs
            run.Style = s2
        Next

        '' Done:
        Return doc
    End Function
End Class