HyperlinkFieldsHelpers.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 HYPERLINK fields,
'' using content creation helper methods.
Public Class HyperlinkFieldsHelpers
    Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()

        '' 0. Paragraph with an external hyperlink:
        Dim p = doc.Body.AddParagraph(
            "Among other things, fields allow to insert hyperlinks into documents. " &
            "Following is a hyperlink to a web address. ")
        Dim hl0 = p.AddSimpleField(
            "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 = doc.Body.AddParagraph("Next is another hyperlink, this time with its own style and a custom tooltip. ")
        Dim hl1 = p.AddSimpleField(
            "HYPERLINK ""https://developer.mescius.com/"" \o ""Click to open MESCIUS web page"" \t ""_blank""",
            "Click to go to developer.mescius.com.")

        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:
        Dim bmkTop = "BookmarkTop"
        Dim bmkBot = "BookmarkEnd"
        p = doc.Body.AddParagraph(
            "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. ")
        p.GetRange().Bookmarks.Add(bmkTop)
        Dim hl2 = p.AddSimpleField(
            $"HYPERLINK \l ""{bmkBot}"" \o ""Jumo to {bmkBot}""",
            $"Click to jump to {bmkBot} at the end of the document.")
        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

        For i As Integer = 0 To 99
            doc.Body.AddParagraph($"Filler paragraph {i}.")
        Next
        Dim pb = doc.Body.AddParagraph($"{bmkBot} points here. ")
        pb.GetRange().Bookmarks.Add(bmkBot)
        Dim hl3 = pb.AddSimpleField(
            $"HYPERLINK \l ""{bmkTop}"" \o ""Jumo to {bmkTop}""",
            $"Jump back to {bmkTop}.")
        For Each run In hl3.GetRange().Runs
            run.Style = s2
        Next

        Return doc
    End Function
End Class