Free Support Forum - aspose.com

Hyperlink Font is incorrect after Inserting it into Document using NET

Hi!

We have found an issue when inserting a hyperlink into word document and then saving it as HTML. Some style values are not applied to the final hyperlink in HTML.
Here is sample app: WordTest.zip (19.7 KB)

To reproduce the problem:

  1. Open project in VisualStudio
  2. Run the app. Select destination folder and press a button.
  3. In the destination folder you’ll see two html files:
    3a. asposeGood.html - this is correct file. Hyperlinks are green with Comic Sans font applied.
    3b. asposeWrong.html - here you can see that created hyperlinks have wrong font.

The word document attached to the application has a style for hyperlinks - they should be green with Comic Sans font.
However, when I use this code:

builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
builder.InsertHyperlink(text, tag, false);

Then I can see that my hyperlink gets every setting from Word’s style except font name.

So when I use:

builder.PushFont();
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
var style = builder.Document.Styles[“Hyperlink”];
builder.Font.Name = style.Font.Name;
builder.InsertHyperlink(text, tag, false);
builder.PopFont();

Then my final hyperlink is styled correctly.

Style for those hyperlinks is created in word document.

Please, let me know if you need some more information.
Thanks!

@ManMasterDevelopment

Please clear the font and paragraph formatting as show below to get the desired output.

builder.Font.ClearFormatting();
builder.ParagraphFormat.ClearFormatting();

public void CreateHyperlinksBad(Document doc, List<string> tags)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    foreach (var tag in tags)
    {
        var bookmark = doc.Range.Bookmarks[tag];
        if (bookmark != null)
        {
            var text = bookmark.Text;
            bookmark.Text = string.Empty;
            builder.MoveToBookmark(tag, false, true);
            bookmark.Remove();

            builder.Font.ClearFormatting();
            builder.ParagraphFormat.ClearFormatting();
            builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
            builder.InsertHyperlink(text, tag, false);

        }
    }
}