Hyperlinks.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 insert hyperlinks into a Word document
  14. // using the Range.Hyperlinks collection.
  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 HyperlinkFields,
  18. // but unlike that sample uses hyperlinks collections
  19. // rather than HYPERLINK fields.
  20. public class Hyperlinks
  21. {
  22. public GcWordDocument CreateDocx()
  23. {
  24. var doc = new GcWordDocument();
  25. var section = doc.Body.Sections.First;
  26. var pars = section.GetRange().Paragraphs;
  27.  
  28. // 0. Paragraph with an external hyperlink:
  29. var p = pars.Add(
  30. "It is easy to add hyperlinks to document content via Range.Hyperlinks collection. " +
  31. "Following is a hyperlink to a web address. ");
  32. // There are different Hyperlinks.Add() overloads that allow to specify
  33. // different hyperlink options such as screen tips etc:
  34. var hl0 = p.GetRange().Hyperlinks.Add(new Uri("http://www.google.com"), null, "Click to go to www.google.com.");
  35.  
  36. // 1. Paragraph with an external hyperlink with its own style:
  37. p = pars.Add("Next is another hyperlink, this time with its own style and a custom tooltip. ");
  38. var hl1 = p.GetRange().Hyperlinks.Add(new Uri("https://www.grapecity.com/en/"), null, "Click to go to www.grapecity.com.", "Click to open GrapeCity web page");
  39.  
  40. // 2. Link to a bookmark within the document:
  41. // We add bookmarks at the top and bottom of the document,
  42. // and set up links to jump between them.
  43. var bmkTop = "BookmarkTop";
  44. var bmkBot = "BookmarkEnd";
  45. p = pars.Add(
  46. "Hyperlinks can also point to locations within the document. " +
  47. "We add some filler paragraphs below, followed by a paragraph " +
  48. $"with a bookmark named '{bmkBot}' attached to it. " +
  49. "The next hyperlink jumps to that bookmark. ");
  50. // Attach a bookmark to this paragraph so we can jump back here:
  51. p.GetRange().Bookmarks.Add(bmkTop);
  52. // A hyperlink to a bookmark:
  53. var hl2 = p.GetRange().Hyperlinks.Add($"{bmkBot}", $"Click to jump to {bmkBot} at the end of the document.", $"Jumo to {bmkBot}");
  54. hl2.GetRange().Runs.First.Style = doc.Styles[BuiltInStyleId.FollowedHyperlink];
  55.  
  56. // Add filler, bookmarked paragraph after it, and
  57. // a link to jump back:
  58. for (int i = 0; i < 100; ++i)
  59. pars.Add($"Filler paragraph {i}.");
  60. var pb = pars.Add($"{bmkBot} points here. ");
  61. pb.GetRange().Bookmarks.Add(bmkBot);
  62. var hl3 = pb.GetRange().Hyperlinks.Add($"{bmkTop}", $"Jump back to {bmkTop}.", $"Jumo to {bmkTop}");
  63. hl3.GetRange().Runs.First.Style = doc.Styles[BuiltInStyleId.FollowedHyperlink];
  64.  
  65. // Done:
  66. return doc;
  67. }
  68. }
  69. }
  70.