HyperlinkFields.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.Drawing
  6. Imports GrapeCity.Documents.Word
  7.  
  8.  
  9. '' This sample shows how to add hyperlinks to a
  10. '' Word document using HYPERLNK fileds.
  11. '' It adds some external hyperlinks to Web sites,
  12. '' And also hyperlinks to bookmarks within the document.
  13. '' This sample Is very similar to Hyperlinks,
  14. '' but uses fields rather than hyperlink collections.
  15. Public Class HyperlinkFields
  16. Function CreateDocx() As GcWordDocument
  17. Dim doc = New GcWordDocument()
  18. Dim pars = doc.Body.Paragraphs
  19.  
  20. '' 0. Paragraph with an external hyperlink:
  21. Dim p = pars.Add(
  22. "Among other things, fields allow to insert hyperlinks into documents. " +
  23. "Following is a hyperlink to a web address. ")
  24. '' Append a hyperlink field to it (note that unless we specifically style it,
  25. '' the hyperlink will look just like other text):
  26. Dim hl0 = p.GetRange().SimpleFields.Add(
  27. "HYPERLINK ""http://www.google.com"" \t ""_blank""",
  28. "Click to go to www.google.com.")
  29.  
  30. '' 1. Paragraph with an external hyperlink with its own style:
  31. p = pars.Add("Next is another hyperlink, this time with its own style and a custom tooltip. ")
  32. Dim hl1 = p.GetRange().SimpleFields.Add(
  33. "HYPERLINK ""https://www.grapecity.com/en/"" \o ""Click to open GrapeCity web page"" \t ""_blank""",
  34. "Click to go to www.grapecity.com.")
  35.  
  36. '' Add a style to the 2nd hyperlink:
  37. Dim s1 = doc.Styles.Add("Hyperlink style 1", StyleType.Character)
  38. s1.Font.Color.RGB = Color.Blue
  39. s1.Font.Size += 2
  40. s1.Font.Bold = True
  41. For Each run In hl1.GetRange().Runs
  42. run.Style = s1
  43. Next
  44.  
  45. '' 2. Link to a bookmark within the document:
  46. '' We add bookmarks at the top and bottom of the document,
  47. '' and set up links to jump between them.
  48. Dim bmkTop = "BookmarkTop"
  49. Dim bmkBot = "BookmarkEnd"
  50. p = pars.Add(
  51. "Hyperlinks can also point to locations within the document. " +
  52. "We add some filler paragraphs below, followed by a paragraph " +
  53. $"with a bookmark named '{bmkBot}' attached to it. " +
  54. "The next hyperlink jumps to that bookmark. ")
  55. '' Attach a bookmark to this paragraph so we can jump back here:
  56. p.GetRange().Bookmarks.Add(bmkTop)
  57. '' A hyperlink to a bookmark:
  58. Dim hl2 = p.GetRange().SimpleFields.Add(
  59. $"HYPERLINK \l ""{bmkBot}"" \o ""Jumo to {bmkBot}""",
  60. $"Click to jump to {bmkBot} at the end of the document.")
  61. '' Add a style for the 2nd hyperlink:
  62. Dim s2 = doc.Styles.Add("Hyperlink style 2", StyleType.Character)
  63. s2.Font.Color.RGB = Color.DarkOrange
  64. s2.Font.Size += 1
  65. s2.Font.Italic = True
  66. For Each run In hl2.GetRange().Runs
  67. run.Style = s2
  68. Next
  69.  
  70. '' Add filler, bookmarked paragraph after it, and
  71. '' a link to jump back:
  72. For i = 0 To 99
  73. pars.Add($"Filler paragraph {i}.")
  74. Next
  75. Dim pb = pars.Add($"{bmkBot} points here. ")
  76. pb.GetRange().Bookmarks.Add(bmkBot)
  77. Dim hl3 = pb.GetRange().SimpleFields.Add(
  78. $"HYPERLINK \l ""{bmkTop}"" \o ""Jumo to {bmkTop}""",
  79. $"Jump back to {bmkTop}.")
  80. For Each run In hl3.GetRange().Runs
  81. run.Style = s2
  82. Next
  83.  
  84. '' Done:
  85. Return doc
  86. End Function
  87. End Class
  88.  
  89.