Hyperlinks.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This sample shows how to insert hyperlinks into a Word document
    // using the Range.Hyperlinks collection.
    // It adds some external hyperlinks to Web sites,
    // and also hyperlinks to bookmarks within the document.
    // This sample is very similar to HyperlinkFields,
    // but unlike that sample uses hyperlinks collections
    // rather than HYPERLINK fields.
    public class Hyperlinks
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();
            var section = doc.Body.Sections.First;
            var pars = section.GetRange().Paragraphs;

            // 0. Paragraph with an external hyperlink:
            var p = pars.Add(
                "It is easy to add hyperlinks to document content via Range.Hyperlinks collection. " +
                "Following is a hyperlink to a web address. ");
            // There are different Hyperlinks.Add() overloads that allow to specify
            // different hyperlink options such as screen tips etc:
            var hl0 = p.GetRange().Hyperlinks.Add(new Uri("http://www.google.com"), null, "Click to go to www.google.com.");

            // 1. Paragraph with an external hyperlink with its own style:
            p = pars.Add("Next is another hyperlink, this time with its own style and a custom tooltip. ");
            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");

            // 2. Link to a bookmark within the document:
            // We add bookmarks at the top and bottom of the document,
            // and set up links to jump between them.
            var bmkTop = "BookmarkTop";
            var bmkBot = "BookmarkEnd";
            p = pars.Add(
                "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. ");
            // Attach a bookmark to this paragraph so we can jump back here:
            p.GetRange().Bookmarks.Add(bmkTop);
            // A hyperlink to a bookmark:
            var hl2 = p.GetRange().Hyperlinks.Add($"{bmkBot}", $"Click to jump to {bmkBot} at the end of the document.", $"Jumo to {bmkBot}");
            hl2.GetRange().Runs.First.Style = doc.Styles[BuiltInStyleId.FollowedHyperlink];

            // Add filler, bookmarked paragraph after it, and
            // a link to jump back:
            for (int i = 0; i < 100; ++i)
                pars.Add($"Filler paragraph {i}.");
            var pb = pars.Add($"{bmkBot} points here. ");
            pb.GetRange().Bookmarks.Add(bmkBot);
            var hl3 = pb.GetRange().Hyperlinks.Add($"{bmkTop}", $"Jump back to {bmkTop}.", $"Jumo to {bmkTop}");
            hl3.GetRange().Runs.First.Style = doc.Styles[BuiltInStyleId.FollowedHyperlink];

            // Done:
            return doc;
        }
    }
}