Insert HTML in Word Document in Bookmark using C#

Hello there

Recently bookmarks lose styling when filled with InsertHtml method

My C# program reads a Word document and insert HTML in Word document inside Bookmarks.

Since May, it worked perfectly. The styling of each bookmark on the document (font, size, color…) was applied.

But not anymore despite no change in my code. Every bookmark is in the same font (Times new roman), same size (12), same color (black).
The inner html styling (balises <b></b> are bold etc…) works fine still.

I download the newest DLL of Aspose.Words for .NET but no luck.

Any ideas on the cause of a sudden change?

Thanks

@cliaco,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source Word DOCX document containing the Bookmarks
  • HTML file (Copy HTML string in a HTML file) that you want to insert in Word Document
  • Aspose.Words 21.10 generated DOCX file showing the undesired output
  • Your expected DOCX file showing the desired output. You can create this file manually by using an old version of Aspose.Words for .NET.
  • Please also create a standalone simplified Console Application (source code without compilation errors) that helps us to reproduce this problem during inserting HTML in Word document on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will then start further investigation into your particular scenario and provide you more information.

asposeRequest.zip (6.2 MB)

Hello, thanks for your reply.

You will find in the attached zip :

  • a model with different colored and various sized bookmarks.
  • a bad result generated by my app (all black, all small, all times new roman)
  • a console app without the DLL. You’ll find the result in the output folder. And the model is already inside the input folder. The result should be in small black times new roman type despite my model styling

I cannot send you the desired output because I can’t get my app to create them even with older version of Aspose.

Thanks again

@cliaco,

Please also provide an expected Word DOCX file showing the desired output here for our reference. You can create this file manually by using MS Word. Please also list the steps that you performed in MS Word to create the expected file. We will then provide you C# code to insert HTML in Word Document in Bookmark by using Aspose.Words for .NET.

expected_result.zip (5.9 KB)

Hello,

Here you go, I’ve manually recreated what I expect with the input provided.
The steps are as follow:
I took the HTML data I want the bookmark to be filled with and I replace the bookmark with it
Aspose.Words was working fine, I could do it with a Document Builder and InsertHtml()

Just to be sure, as I am afraid my not-so-good-English may have warp what I need : I don’t want exactly the expected_result.doc. My C# program is dynamic, whatever style is used on the .doc/.docx bookmark needs to stay when filled (inserting HTML) with Aspose.

Thanks again for your input and help.

@cliaco,

Please try to use the following overloads:

I have updated your following method:

C# Code to Insert HTML in Word Document in Bookmark

using Aspose.Words;
using System;

namespace Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set license before inserting HTML in Word Document in Bookmark
            ApplyLicense();
            // Load Word file
            Document word = new Document("word.docx");
            string html = "<b>Insert HTML in Word Document in Bookmark using C#</b>";
            insertContenu(ref word, "MyBookmark", html);
        }
        private static void insertContenu(ref Document oWordDoc,
                                          string oBookMark, string contenu)//, string directoryWord)
        {
            try
            {
                DocumentBuilder dBuilder = new DocumentBuilder(oWordDoc);
                Bookmark aBookmark = oWordDoc.Range.Bookmarks[oBookMark];
                if (aBookmark != null)
                {
                    aBookmark.Text = "";
                    dBuilder.MoveToBookmark(aBookmark.Name, true, true);
                    var para = (Paragraph)aBookmark.BookmarkStart.ParentNode;
                    dBuilder.CurrentParagraph.ParagraphFormat.SpaceAfter = para.ParagraphFormat.SpaceAfter;
                    dBuilder.InsertHtml(contenu, true);

                    if (oBookMark.Contains("t_"))
                    {
                        var prev = oWordDoc.Range.Bookmarks[oBookMark].BookmarkStart.ParentNode.PreviousSibling;
                        if (prev != null && (prev.Range.Text == "" || prev.Range.Text == "\r"))
                        {
                            oWordDoc.Range.Bookmarks[oBookMark].BookmarkStart.ParentNode.PreviousSibling.Remove();
                        }
                    }
                }
            }
            catch (Exception e) { string error = e.ToString(); }
        }
        public static void ApplyLicense()
        {
            License lic = new License();
            lic.SetLicense("path to .lic");
        }
    }
}

It works ! Thanks a billion.

Have a nice day

1 Like