Insert Paragraph, Table, HMTL into existing Text or Rich Text Content Control

We have docx files that have existing Text or Rich Text controls.

Using java Aspose.words.2406, I can only append the Run to these existing Content Controls.

For Paragraph or Table or using DocumentBuilder to insertHTML, got the error below.
Cannot insert a node of this type at this location.

The Markup.level in these existing Text controls = MarkupLevel.Inline

I saw some older posts related to similar issues. However, try those examples did not work either.

What do I need to do to be able to insert the Paragraph, Table or HTML, etc.?
Is there a way to change the Markup.level?

I have attached the sample docx that I testing with. There are couple Rich Text in there.
ContControlTest.7z (32.6 KB)

Thank You

@nlee08

To insert a paragraph, table, or HTML content into existing Text or Rich Text Content Controls using Aspose.Words for Java, you need to be aware of the limitations imposed by the markup level of the structured document tags (SDTs). Since you mentioned that the markup level is set to MarkupLevel.Inline, this restricts the types of nodes you can insert directly into these controls.

Steps to Insert Content into Content Controls

  1. Change the Markup Level: If you want to insert block-level elements like paragraphs or tables, you should consider changing the markup level of your structured document tag to MarkupLevel.Block. This allows you to insert block-level nodes.

  2. Using DocumentBuilder: You can utilize the DocumentBuilder class to insert content into the document. Here’s an example of how to create a Rich Text Content Control and insert a paragraph and a table into it:

import com.aspose.words.*;

public class InsertContentIntoContentControl {
    public static void main(String[] args) throws Exception {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Create a Rich Text Content Control
        StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK);
        builder.insertNode(sdtRichText);

        // Insert a paragraph into the Rich Text Content Control
        Paragraph para = new Paragraph(doc);
        para.appendChild(new Run(doc, "This is a paragraph inside the Rich Text Content Control."));
        sdtRichText.appendChild(para);

        // Insert a table into the Rich Text Content Control
        Table table = builder.startTable();
        builder.insertCell();
        builder.write("Cell 1");
        builder.insertCell();
        builder.write("Cell 2");
        builder.endRow();
        builder.endTable();
        sdtRichText.appendChild(table);

        // Save the document
        doc.save("Output.docx");
    }
}

Important Notes

  • Markup Level: If you need to insert block-level elements, ensure that the SDT’s markup level is set to MarkupLevel.Block. You can do this when creating the SDT.
  • Error Handling: If you encounter errors like “Cannot insert a node of this type at this location,” it typically indicates that the node type you are trying to insert is not compatible with the current markup level of the SDT.

By following these guidelines, you should be able to successfully insert paragraphs, tables, or HTML content into your existing content controls. If you have any further questions or need additional examples, feel free to ask!

Can anyone follow up on this?

I want to insert the html table into one of the existing StructuredDocumentTag that has the tag name ‘WorkflowSignoffTable’. (See attached docx file from original post).

Currently, I could not figure out a way to to do.

Any help/suggestion will be be great help.

Thank You

FYI, for now, to make it work, I have to update the docx file 's content control properties to Show as: None.

@nlee08 As mentioned above, WorkflowSignoffTable structured document tag is inline. So it can contain only inline nodes, such as Run. On other hand, the nodes you would like to inset are paragraphs and tables, which are block level nodes. So to make it work, you should change WorkflowSignoffTable structured document tag markup level.

unfortunately, there is no way to change SDT markup level, you can only remove old SDT and insert new one with required markup level.

Got it. Thank You.

1 Like