When adding an HTML h1, index changes

Hi guys,
I have the following code:

    final Document doc = new Document(BASE_PATH + "Test Report.docx");
    final DocumentBuilder documentBuilder = new DocumentBuilder(doc);
    final Cell cell = (Cell) documentBuilder.getCurrentSection()
            .getChildNodes(NodeType.CELL, true)


    documentBuilder.insertHtml("<h1>This is a header test</h1>");
    doc.save(BASE_PATH + "result.docx");

Files.zip (67.2 KB)

and the index of result.docx has the “This is a header test” text as another header (we have a scenario where this text also has the numbering before).
My question is: Is this text suppose to be on the index? Why in some cases these fields keep the numbering and other cases don’t?

Best regards,
Hugo Freixo


Please use DocumentBuilder.InsertHtml method (String html, Boolean useBuilderFormatting) as shown below to get the desired output.

documentBuilder.insertHtml("<h1>This is a header test</h1>", true);

When useBuilderFormatting is false, DocumentBuilder formating is ignored and formatting of inserted text is based on default HTML formatting. As a result, the text looks as it is rendered in browsers.

When useBuilderFormatting is true, formatting of inserted text is based on DocumentBuilder formatting, and the text looks as if it were inserted with Write.