EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
279 stars 49 forks source link

Allows applying custom styling to Hyperlinks #100

Closed PrzemyslawKlys closed 1 year ago

PrzemyslawKlys commented 1 year ago

This PR aims to resolve

Hyperlinks are a bit like Paragraphs. This means that they have their own Runs, Texts and RunProperties. This PR tries to address this. When users interacts with WordParagraph and the properties such as Bold, Italic (and so on) are applied, and the WordParagraph contains a HyperLink the style will be applied to Hyperlink instead of applying it to a new Run that would not have any impact on styling.

internal static void Example_BasicWordWithHyperLinks(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with hyperlinks");
    string filePath = System.IO.Path.Combine(folderPath, "BasicDocumentHyperlinks.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {
        document.AddParagraph("Test 1");

        document.AddParagraph("Hello users! Please visit ").AddHyperLink("bookmark below", "TestBookmark", true, "This is link to bookmark below shown within Tooltip");
        Console.WriteLine(document.HyperLinks.Count);
        Console.WriteLine(document.Sections[0].ParagraphsHyperLinks.Count);
        Console.WriteLine(document.ParagraphsHyperLinks.Count);
        Console.WriteLine(document.Sections[0].HyperLinks.Count);
        document.AddParagraph("Test HYPERLINK ").AddHyperLink(" to website?", new Uri("https://evotec.xyz"), true);

        // this hyperlink will be styled with defaults, but then changed a bit
        var test = document.AddParagraph("Test Email Address ").AddHyperLink("Przemysław Klys", new Uri("mailto:kontakt@evotec.pl?subject=Test Subject"), true);
        test.Bold = true;
        test.Italic = true;
        test.Underline = UnderlineValues.Dash;
        test.Color = Color.Green;

        // this hyperlink will have no style at all
        document.AddParagraph("Test HYPERLINK ").AddHyperLink(" to website?", new Uri("https://evotec.xyz"));

        // lets style next hyperlink with orange color and make it 20 in size
        var anotherHyperlink = document.AddParagraph("Test HYPERLINK ").AddHyperLink(" to website?", new Uri("https://evotec.pl"));
        anotherHyperlink.Color = Color.Orange;
        anotherHyperlink.FontSize = 20;

        //document.HyperLinks.Last().Remove();

        document.AddParagraph("Test 2").AddBookmark("TestBookmark");
        document.AddParagraph("Hello users! Please visit ").AddHyperLink("bookmark below", "TestBookmark", true, "This is link to bookmark below shown within Tooltip");

        document.HyperLinks.Last().Uri = new Uri("https://evotec.pl");
        document.HyperLinks.Last().Anchor = "";

        Console.WriteLine(document.HyperLinks.Count);

        document.Save(openWord);
    }
}

This supports basic Hyperlinks. It's possible for one hyperlink to be styled in a way where even every letter of it has it's own Run, Text, RunProperties, but supporting this in this simplified model seems a bit excessive.