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