DocumentBuilder.InsertHtml does not work

Hi,

I am trying to insert HTML in place of a content control. It is working in most cases but in this particular document\scenario, it does not work. I have attached the document. Here is the code

void InsertHTMLusingAsposeMemoryV2()
{
    byte[] fileByteArray = File.ReadAllBytes("C:\\HTML_Simple.docx");

    var ms = new MemoryStream();

    ms.Write(fileByteArray, 0, fileByteArray.Length);

    ms.Seek(0, SeekOrigin.Begin);

    Document doc = new Document(ms);
    DocumentBuilder builder = new DocumentBuilder(doc);
    const string html = "<html><body>Hello World</body></html>";
    string path = "ns0:TCSVerify[1]/ns0:vTwoLevelRepeat[1]/ns0:vTwoLevelData1";
    StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChildNodes(NodeType.StructuredDocumentTag, true).FirstOrDefault(item => ((StructuredDocumentTag)item).XmlMapping.XPath == path);

    builder.MoveTo(sdt);

    builder.InsertHtml(html, Aspose.Words.HtmlInsertOptions.UseBuilderFormatting | Aspose.Words.HtmlInsertOptions.RemoveLastEmptyParagraph);

    sdt.Remove();

    builder.Document.Save("C:\\output_Simple.docx");
}

HTML_SimpleRepeating.docx (14.3 KB)

@imran.khan1 As I can see HTML is properly inserted into SDT, but after insertion SDT is removed, so the output document does not contain in the inserted content. Please try commenting the following line of code:

sdt.Remove();

Tried that and output is attached. Here is the code:

void InsertHTMLusingAsposeMemoryV2()
{
    byte[] fileByteArray = File.ReadAllBytes("C:\\HTML_SimpleRepeating.docx");

    var ms = new MemoryStream();

    ms.Write(fileByteArray, 0, fileByteArray.Length);

    ms.Seek(0, SeekOrigin.Begin);

    Document doc = new Document(ms);
    DocumentBuilder builder = new DocumentBuilder(doc);
    const string html = "<html><body>Hello World</body></html>";
    string path = "ns0:TCSVerify[1]/ns0:vTwoLevelRepeat[1]/ns0:vTwoLevelData1";
    StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChildNodes(NodeType.StructuredDocumentTag, true).FirstOrDefault(item => ((StructuredDocumentTag)item).XmlMapping.XPath == path);

    builder.MoveTo(sdt);
    
    builder.InsertHtml(html, Aspose.Words.HtmlInsertOptions.UseBuilderFormatting | Aspose.Words.HtmlInsertOptions.RemoveLastEmptyParagraph);

    // sdt.Remove();

    builder.Document.Save("C:\\output_SimpleRepeating.docx");
}

@imran.khan1 Here is output produced on my side using the latest version of Aspose.Words:

I am using .Net Core 6.0.1 with Aspose 23.6.0.

Can we have a quick call ? I can show you how it is working on my side.

@imran.khan1 Unfortunately, we do not provide support via phone or video calls. The main place for getting support is this forum.
I have tested the same code in .NET6 application and get the same output I have provided in my previous answer.

Then send me your project along with the docx file you’re trying.

@imran.khan1 I have used the following code for testing:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
const string html = "<html><body>Hello World</body></html>";
string path = "ns0:TCSVerify[1]/ns0:vTwoLevelRepeat[1]/ns0:vTwoLevelData1";
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChildNodes(NodeType.StructuredDocumentTag, true).FirstOrDefault(item => ((StructuredDocumentTag)item).XmlMapping.XPath == path);

builder.MoveTo(sdt);

builder.InsertHtml(html, Aspose.Words.HtmlInsertOptions.UseBuilderFormatting | Aspose.Words.HtmlInsertOptions.RemoveLastEmptyParagraph);

doc.Save(@"C:\Temp\out.docx");

And the document you have attached earlier as an input: HTML_SimpleRepeating.docx (14.3 KB)

There is a major difference in your code and mine. You’re creating “doc” object from a docx file. My requirements is to create the “doc” object from a memory stream.

Try this in your code and you will see the issue

byte[] fileByteArray = File.ReadAllBytes("C:\\HTML_SimpleRepeating.docx");
var ms = new MemoryStream();
ms.Write(fileByteArray, 0, fileByteArray.Length);
ms.Seek(0, SeekOrigin.Begin);
Document doc = new Document(ms);

@imran.khan1
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-25599

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

We have a paid support service. What information do you need to expedite the fix of this issue ?

@imran.khan1 You should escalate the issue in the Paid Support Helpdesk. Them my colleagues from Paid Support team will raise the issue priority.

The issues you have found earlier (filed as WORDSNET-25599) have been fixed in this Aspose.Words for .NET 24.1 update also available on NuGet.