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

Unable to format external hyperlinks #90

Closed dstringvc closed 1 year ago

dstringvc commented 1 year ago

I'm trying to format external hyperlinks with custom font color and size, no underline, but cannot get anything other than the default font from the document.

I have tried setting the font attributes inline: document.AddHyperLink("Go to Evotec Blogs", new Uri("https://evotec.xyz")).SetColorHex("1C8FD6").SetFontSize(14).SetBold();

I have tried setting the font attributes after the fact: var paragraphWithHyperLink = document.AddHyperLink("Go to Evotec Blogs", new Uri("https://evotec.xyz")); paragraphWithHyperLink.SetColorHex("1C8FD6").SetFontSize(14).SetBold();

I have tried creating the hyperlink off a new paragraph: WordParagraph paragraph = document.AddParagraph(); paragraph.AddHyperLink("Go to Evotec Blogs", new Uri("https://evotec.xyz")).SetColorHex("1C8FD6").SetFontSize(14).SetBold();

None of these attempts have worked. Am I missing something? Thank you!

PrzemyslawKlys commented 1 year ago

Neither can I, will see what's wrong with it. It seems we're applying it to another run, instead of the already existing one within HyperLink. This may require some changes or maybe application styles directly to hyperlink

image

dstringvc commented 1 year ago

Any updates on the hyperlink issue? Is there anything I can do to help with the troubleshooting? Thank you!

PrzemyslawKlys commented 1 year ago

I did play with it. I wanted HyperLink to inherit properties from Paragraph, so that it would have all the options from Paragraph (settable on the hyperlink itself). Unfortunetly my knowledge on inheritance and how to make some things is very limited, so I was struggling to fix it. I may as well copy code from Paragraphs (all properties/methods that touch RUnProperties on Paragraph) and let it be a "copy", that later on can be improved by someone who's better coder.

Generally the problem is that Hyperlinks can contain runs and runs contains runproperties which is what makes the hyperlink styling work. We don't have that now. What we have now happens on the run that's outside of hyperlink, but within paragraph.

image

As with OpenXML usual approach it's possible that one Link, or a more the Text it binds to it's possible is split over multiple runs (think of a link that has multiple colors - not your standard, but it could happen).

So there's no need to troubleshoot anything - more like help is needed to fix it properly, because as far as I see it - I can only fix it with a workaround, which may work, but should be improved later on.

PrzemyslawKlys commented 1 year ago

It took a bit longer than expected. But it's now merged.