Insert rich text content control on a specific paragraph

Hi,

I need to add rich text content control on a specific paragraph in the word document. When i add the content control , it is not placed at the correct location and corrupts document structure. Can you guide me to a sample code.

Your help is appreciated. Its highly urgent . We are using a paid licence and are already live on production.

Thanks

@saurabh.arora,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 19.7 generated output document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word.
  • Please also create a simplified standalone console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

Thanks for the reply.

Please find the documents zipped.

Test.docx is input docx and Test123.docx is the output docx. My aim is to create rich text content control over paragraph whose length is greater than 150.

If you will see the output docx , you will observe the structure has changed. I want to retain document structure.

Documents.zip (22.6 KB)

Code :

public static void main(String… args) throws Exception {

Document document = new Document("/home/sauravarora/Downloads/Test.docx");

Integer i = 1;

for (Paragraph para : (Iterable<Paragraph>) document.getChildNodes(NodeType.PARAGRAPH, true)) {

if ((StringUtils.isEmpty(para.getText()) || StringUtils.isEmpty(para.getText().trim()) || para.getText().trim().length() <= 150 || para.getText().matches(IGNORE_TEXT_IDENTIFIERS) || (para.getParentNode() != null && IGNORE_NODE_TYPES.contains(para.getParentNode().getNodeType())))) {
continue;
}

StructuredDocumentTag sdtRichText = new StructuredDocumentTag(document, SdtType.RICH_TEXT, MarkupLevel.BLOCK);

sdtRichText.removeAllChildren();

sdtRichText.setTag(“Test” + i);
sdtRichText.setTitle(“Test Content Control”);
sdtRichText.isShowingPlaceholderText(false);
i++;
sdtRichText.appendChild(para);
document.getFirstSection().getBody().appendChild(sdtRichText);
}

document.save("/home/sauravarora/Downloads/Test123.docx");
}

@saurabh.arora,

You can build logic on the following code to get the desired output:

Document document = new Document("E:\\Documents\\Test.docx");

Integer i = 1;
for (Paragraph para : (Iterable<Paragraph>) document.getChildNodes(NodeType.PARAGRAPH, true)) {

    if (para.getText().trim().length() <= 150) {
        continue;
    }

    StructuredDocumentTag sdtRichText = new StructuredDocumentTag(document, SdtType.RICH_TEXT, MarkupLevel.BLOCK);
    sdtRichText.removeAllChildren();

    sdtRichText.setTag("Test" + i);
    sdtRichText.setTitle("Test Content Control");
    sdtRichText.isShowingPlaceholderText(false);
    i++;

    para.getParentNode().insertBefore(sdtRichText, para);
    sdtRichText.appendChild(para);
}

document.save("E:\\Documents\\awjava-19.7.docx");
1 Like