In aspose we want to update html on inline line tags

In aspose we want to update html on inline line tags, but unable to getting exception Cannot insert a node of this type at this location.

package com.sirionlabs.api;

import com.aspose.words.*;

import java.io.FileInputStream;
import java.io.InputStream;

public class Main10 {
    //https://forum.aspose.com/t/issue-while-converting-word-document-in-html/284634/1
    public static void main(String[] args) throws Exception {
        License license = new License();
        license.setLicense(new FileInputStream("/home/mohitkumar/IdeaProjects/contract-authoring/auto-tagging/target/test-classes/aspose-licence"));

        Document document;
        InputStream stream = null;
       // try {
            document = new Document("/home/mohitkumar/Downloads/InlineIssue.docx");
            DocumentBuilder documentBuilder = new DocumentBuilder(document);

            for (Object st : document.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {
                StructuredDocumentTag std = (StructuredDocumentTag) st;
                if (std.getSdtType() == SdtType.RICH_TEXT) {
                    System.out.println(std.getText());
                    updateContentControl(document, documentBuilder, "<!DOCTYPE html>\n" +
                            "<html lang=\"en\">\n" +
                            "<head>\n" +
                            "    <meta charset=\"UTF-8\">\n" +
                            "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                            "    <title>Sample HTML Document</title>\n" +
                            "</head>\n" +
                            "<body>\n" +
                            "    <p>This is a sample paragraph with some text. You can add more content here to see how it looks on the web page.</p>\n" +
                            "</body>\n" +
                            "</html>\n", std);
                }
            }

            document.save("/home/mohitkumar/Downloads/FixedException.docx");

    }

    private static void updateContentControl(Document document, DocumentBuilder documentBuilder, String modifiedText, StructuredDocumentTag std) throws Exception {


        if (MarkupLevel.INLINE == std.getLevel() ) {
            Node firstChild = std.getFirstChild();
            com.aspose.words.List list = null;

            if (firstChild instanceof Paragraph) {
                Paragraph firstPara = (Paragraph) firstChild;
                if (firstPara.isListItem()) {
                    list = firstPara.getListFormat().getList();
                }
            }
            std.removeAllChildren();
            Paragraph para = new Paragraph(document);
            std.appendChild(para);
            documentBuilder.moveTo(std.getFirstChild());
            documentBuilder.insertHtml(modifiedText, false);
            para.getListFormat().setList(list);

            Node lastChild = std.getLastChild();
            if (("\r").equals(lastChild.getText())) {
                std.removeChild(lastChild);
            }

        }
    }

}

InlineIssue.docx (30.0 KB)

@mohitkumar1991 You need to use Block level SDT, it is no allowed to insert paragraphs in inline SDT.

I just need to update inline content control with html, could you provide the solution to update?
I can replace/update sdt.

@mohitkumar1991 You can try to use following code without paragraphs to update inline sdt:

Document document = new Document("InlineIssue.docx");
DocumentBuilder documentBuilder = new DocumentBuilder(document);

for (Object st : document.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {
    StructuredDocumentTag std = (StructuredDocumentTag) st;
    if (std.getSdtType() == SdtType.RICH_TEXT) {
        System.out.println(std.getText());
        updateContentControl(document, documentBuilder, "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>Sample HTML Document</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    This is a sample paragraph with some text. You can add more content here to see how it looks on the web page.\n" +
                "</body>\n" +
                "</html>\n", std);
    }
}

document.save("output.docx");

private static void updateContentControl(Document document, DocumentBuilder documentBuilder, String modifiedText, StructuredDocumentTag std) throws Exception {
    if (MarkupLevel.INLINE == std.getLevel() ) {
        std.removeAllChildren();
        Run run = new Run(document);
        std.appendChild(run);
        documentBuilder.moveTo(std.getFirstChild());
        documentBuilder.insertHtml(modifiedText);

        Node lastChild = std.getLastChild();
        if (("\r").equals(lastChild.getText())) {
            std.removeChild(lastChild);
        }
    }
}