HyperlinkFieldsHelpers.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 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.
    // The code is the same as in the HyperlinkFields example, but
    // uses the (new in v6.2) content creation helper methods, which in most cases yield more robust and compact code,
    // see methods called AddParagraph(), AddRun() etc. on various content types.
    public class HyperlinkFieldsHelpers
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            // 0. Paragraph with an external hyperlink:
            var p = doc.Body.AddParagraph(
                "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.AddSimpleField(
                "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 = doc.Body.AddParagraph("Next is another hyperlink, this time with its own style and a custom tooltip. ");
            var hl1 = p.AddSimpleField(
                "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 = doc.Body.AddParagraph(
                "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.AddSimpleField(
                $"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)
                doc.Body.AddParagraph($"Filler paragraph {i}.");
            var pb = doc.Body.AddParagraph($"{bmkBot} points here. ");
            pb.GetRange().Bookmarks.Add(bmkBot);
            var hl3 = pb.AddSimpleField(
                $"HYPERLINK \\l \"{bmkTop}\" \\o \"Jumo to {bmkTop}\"",
                $"Jump back to {bmkTop}.");
            foreach (var run in hl3.GetRange().Runs)
                run.Style = s2;

            // Done:
            return doc;
        }
    }
}