// This code is part of Document Solutions for Word samples.
// 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 add hyperlinks to a
    // Word document using HYPERLNK fields.
    // It adds some external hyperlinks to Web sites,
    // and also hyperlinks to bookmarks within the document.
    // This sample is very similar to Hyperlinks,
    // but uses fields rather than hyperlink collections.
    public class HyperlinkFields
        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(
                "Among other things, fields allow to insert hyperlinks into documents. " +
                "Following is a hyperlink to a web address. ");
            // Append a hyperlink field to it (note that unless we specifically style it, 
            // the hyperlink will look just like other text):
            var hl0 = p.GetRange().SimpleFields.Add(
                "HYPERLINK \"http://www.google.com\" \t \"_blank\"",
                "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().SimpleFields.Add(
                "HYPERLINK \"https://developer.mescius.com/\" \\o \"Click to open MESCIUS web page\" \t \"_blank\"",
                "Click to go to developer.mescius.com.");

            // Add a style to the 2nd hyperlink:
            var s1 = doc.Styles.Add("Hyperlink style 1", StyleType.Character);
            s1.Font.Color.RGB = Color.Blue;
            s1.Font.Size += 2;
            s1.Font.Bold = true;
            foreach (var run in hl1.GetRange().Runs)
                run.Style = s1;

            // 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:
            // A hyperlink to a bookmark:
            var hl2 = p.GetRange().SimpleFields.Add(
                $"HYPERLINK \\l \"{bmkBot}\" \\o \"Jumo to {bmkBot}\"",
                $"Click to jump to {bmkBot} at the end of the document.");
            // Add a style for the 2nd hyperlink:
            var s2 = doc.Styles.Add("Hyperlink style 2", StyleType.Character);
            s2.Font.Color.RGB = Color.DarkOrange;
            s2.Font.Size += 1;
            s2.Font.Italic = true;
            foreach (var run in hl2.GetRange().Runs)
                run.Style = s2;

            // 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. ");
            var hl3 = pb.GetRange().SimpleFields.Add(
                $"HYPERLINK \\l \"{bmkTop}\" \\o \"Jumo to {bmkTop}\"",
                $"Jump back to {bmkTop}.");
            foreach (var run in hl3.GetRange().Runs)
                run.Style = s2;

            // Done:
            return doc;