HyperlinkFieldsHelpers.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 Word document using HYPERLNK fields.
  14. // It adds some external hyperlinks to Web sites, and also hyperlinks to bookmarks within the document.
  15. // The code is the same as in the HyperlinkFields example, but
  16. // uses the (new in v6.2) content creation helper methods, which in most cases yield more robust and compact code,
  17. // see methods called AddParagraph(), AddRun() etc. on various content types.
  18. public class HyperlinkFieldsHelpers
  19. {
  20. public GcWordDocument CreateDocx()
  21. {
  22. var doc = new GcWordDocument();
  23.  
  24. // 0. Paragraph with an external hyperlink:
  25. var p = doc.Body.AddParagraph(
  26. "Among other things, fields allow to insert hyperlinks into documents. " +
  27. "Following is a hyperlink to a web address. ");
  28. // Append a hyperlink field to it (note that unless we specifically style it,
  29. // the hyperlink will look just like other text):
  30. var hl0 = p.AddSimpleField(
  31. "HYPERLINK \"http://www.google.com\" \t \"_blank\"",
  32. "Click to go to www.google.com.");
  33.  
  34. // 1. Paragraph with an external hyperlink with its own style:
  35. p = doc.Body.AddParagraph("Next is another hyperlink, this time with its own style and a custom tooltip. ");
  36. var hl1 = p.AddSimpleField(
  37. "HYPERLINK \"https://developer.mescius.com/\" \\o \"Click to open MESCIUS web page\" \t \"_blank\"",
  38. "Click to go to developer.mescius.com.");
  39.  
  40. // Add a style to the 2nd hyperlink:
  41. var s1 = doc.Styles.Add("Hyperlink style 1", StyleType.Character);
  42. s1.Font.Color.RGB = Color.Blue;
  43. s1.Font.Size += 2;
  44. s1.Font.Bold = true;
  45. foreach (var run in hl1.GetRange().Runs)
  46. run.Style = s1;
  47.  
  48. // 2. Link to a bookmark within the document:
  49. // We add bookmarks at the top and bottom of the document,
  50. // and set up links to jump between them.
  51. var bmkTop = "BookmarkTop";
  52. var bmkBot = "BookmarkEnd";
  53. p = doc.Body.AddParagraph(
  54. "Hyperlinks can also point to locations within the document. " +
  55. "We add some filler paragraphs below, followed by a paragraph "+
  56. $"with a bookmark named '{bmkBot}' attached to it. " +
  57. "The next hyperlink jumps to that bookmark. ");
  58. // Attach a bookmark to this paragraph so we can jump back here:
  59. p.GetRange().Bookmarks.Add(bmkTop);
  60. // A hyperlink to a bookmark:
  61. var hl2 = p.AddSimpleField(
  62. $"HYPERLINK \\l \"{bmkBot}\" \\o \"Jumo to {bmkBot}\"",
  63. $"Click to jump to {bmkBot} at the end of the document.");
  64. // Add a style for the 2nd hyperlink:
  65. var s2 = doc.Styles.Add("Hyperlink style 2", StyleType.Character);
  66. s2.Font.Color.RGB = Color.DarkOrange;
  67. s2.Font.Size += 1;
  68. s2.Font.Italic = true;
  69. foreach (var run in hl2.GetRange().Runs)
  70. run.Style = s2;
  71.  
  72. // Add filler, bookmarked paragraph after it, and
  73. // a link to jump back:
  74. for (int i = 0; i < 100; ++i)
  75. doc.Body.AddParagraph($"Filler paragraph {i}.");
  76. var pb = doc.Body.AddParagraph($"{bmkBot} points here. ");
  77. pb.GetRange().Bookmarks.Add(bmkBot);
  78. var hl3 = pb.AddSimpleField(
  79. $"HYPERLINK \\l \"{bmkTop}\" \\o \"Jumo to {bmkTop}\"",
  80. $"Jump back to {bmkTop}.");
  81. foreach (var run in hl3.GetRange().Runs)
  82. run.Style = s2;
  83.  
  84. // Done:
  85. return doc;
  86. }
  87. }
  88. }
  89.