Incorrect font when using InsertHtml()

Hi,

I’m using DocumentBuilder along with InsertHtml() and Write() to generate .doc file.

I use InsertHtml() when string contains html tags and use Write() when string doesn’t contain html tag.

The default font is Arial. However, the font always became “Cambria” when using InsertHtml().

Is there a way to avoid the problem from happening?

-------------------------------

DocumentBuilder db = new DocumentBuilder(docTemplate);
if (bookmark != null)
{
db.MoveToBookmark(key[i]);

Regex tagRegex = new Regex(@"<.*?>");
string bookmark_value = value[i];
if (tagRegex.IsMatch(bookmark_value))
{
db.InsertHtml(value[i]);
}
else
{
db.Write(value[i]);
}

}
----------------------------------------------

Thank you

Hi Hong-Yi,

Thanks for your inquiry. Please note that content inserted by DocumentBuilder.InsertHtml method does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified, then default formatting is used for inserted content.

It would be great if you please share following detail for investigation purposes.

  • Please supply us with the input document that is causing the issue
  • Please supply us with the input HTML
  • Please supply us with the output document showing the undesired behavior

Hi,

Thank you for your reply.

Please find the attached documents and the source code below:

“test_template.dot” is the input document. You can find the font type for each bookmark is Arial.

However, after inserting string into those bookmarks by InsertHtml(). The font type became “Cambria”.

On the other hand, for those the strings inserted by Write(), their font types still remained as “Arial”.

Is there a remedy to stop InsertHtml() from using “Cambria”?

Thank you

----------------------------------------------------------------------------------------

Aspose.Words.Document docTemplate = new Aspose.Words.Document(String.Format(@"{0}", file_name));

BookmarkCollection bookmarks = docTemplate.Range.Bookmarks;
DocumentBuilder db = new DocumentBuilder(docTemplate);

foreach (Bookmark bm in bookmarks)
{
if (bm != null)
{
bm.Text = String.Empty;
db.MoveToBookmark(bm.Name);
db.InsertHtml(“Test Aspose.Word 13.6
inserthtml().

  • abcdefghijk
  • 1234567



  • ”);
    db.Write(“Test Aspose.Word 13.6 Write()”);
    }
    }

    docTemplate.Save(@“c:\test.doc”);

    Hi Hong-Yi,

    Thanks for sharing the detail. I have worked with your shared document and have found that the style applied to bookmarks is 'Normal' which have font setting as ('Cambria', 12). Please see the attached image for detail. In your template document, the direct formatting is applied to all bookmarks which is 'Arial', 10. By direct formatting I mean formatting included in addition to the original style which is applied directly onto the paragraph.

    Please note that, content inserted by DocumentBuilder.InsertHtml method does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified, then default formatting is used for inserted content.

    I suggest you please use InsertHtmlWithBuilderFormatting instead of the InsertHtml method as shown in following code snippet. I have attached the code related to InsertHtmlWithBuilderFormatting with this post.

    //builder is DocumentBuilder object
    InsertHtmlWithBuilderFormatting(builder, "

    some text

    "
    );

    Hope this helps you. Please let us know if you have any more queries.

    Thank you very much. The solution is great and my problem is resolved.