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
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.
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!
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).
@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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.