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);

        }
    }
}

I use your solution but still there are cases when the Hyperlink style is not applied correctly. Here you have modified sample application:
WordTest.zip (31.7 KB)
To reproduce the problem:

  1. Open project in VisualStudio
  2. Run the app. Select destination folder and press a button.
  3. In destination folder you will see 3 files. Two are generated by aspose and third is generated by Word application.
    Please take a look on the job text in the html file. When we generate html by word, job text has Hyperlink style applied (green font) but it also has an yellow background.
    When you see asposeGood.html the job looks totally different - local styles are deleted.
    How can we achieve the same result as Word app generates?

@ManMasterDevelopment

We are working over your query and will get back to you soon.

@ManMasterDevelopment

DocumentBuilder.InsertHyperlink method internally calls InsertField to insert an MS Word HYPERLINK field into the document.

Please note that you need to specify font formatting for the hyperlink display text explicitly using the Font property.

Please use the following modified code to get the desired output. You can also set font properties according to your requirement.

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)
        {

            Node node = bookmark.BookmarkStart.NextPreOrder(doc);

            var text = bookmark.Text;
            bookmark.Text = string.Empty;
            builder.MoveToBookmark(tag, false, true);
            bookmark.Remove();

            builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;

            if (node.NodeType == NodeType.Run)
            {
                Run run = (Run)node;
                if (run.Font.HighlightColor != System.Drawing.Color.Empty)
                    builder.Font.HighlightColor = run.Font.HighlightColor;

                builder.Font.Color = doc.Styles[StyleIdentifier.Hyperlink].Font.Color;

            }

            builder.InsertHyperlink(text, tag, false);
        }
    }
}