How to update StructuredDocumentTag conetnt which already bind to xml part

Hi Team,
I want to update content control values at runtime. but StructuredDocumentTag element are already bind to xml in my word file and xml has tags but empty values. now i want to update these values at runtime so that values can reflect in word document.
Below is my code.

private byte[] updateContentControl(InputStream is, IDocument doc) throws Exception {
        if(is == null) return null;
        Document wordDoc = new Document(is);
        for (Object o : wordDoc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {
            StructuredDocumentTag sdt = (StructuredDocumentTag) o;
            switch (sdt.getSdtType()) {
                case SdtType.PLAIN_TEXT: {
                    sdt.removeAllChildren();
                    Paragraph para = sdt.appendChild(new Paragraph(wordDoc));
                    Run run = new Run(wordDoc, doc.getDescriptorValue(sdt.getTitle()));
                    para.appendChild(run);
                    break;
                }
                case SdtType.DROP_DOWN_LIST:
                {

                    SdtListItem listItem = new SdtListItem(doc.getDescriptorValue(sdt.getTitle()));
                    sdt.getListItems().add(listItem);
                    sdt.getListItems().setSelectedValue(listItem);
//                    SdtListItem secondItem = sdt.getListItems().get(2);
//                    sdt.getListItems().setSelectedValue(secondItem);
                    break;
                }
//                case SdtType.PICTURE:
//                {
//                    Shape shape = (Shape) sdt.getChild(NodeType.SHAPE, 0, true);
//                    if (shape.hasImage()) {
//                        shape.getImageData().setImage("Watermark.png");
//                    }
//
//                    break;
//                }
            }
        }
        ByteArrayOutputStream baos =  new ByteArrayOutputStream();
        wordDoc.save(baos,SaveFormat.DOCX);
        return baos.toByteArray();
    }
    private IDocument getTemplateDoc(String documentId){
        if(StringUtils.isEmpty(documentId)) throw new BlueLineException("Template id is null or empty");
        return (IDocument) getDocumentServer().getInformationObjectByID(documentId,getSes());
    }

My xml part file is like :slight_smile:

<?xml version="1.0"?>
<Memo xmlns="http://moro.cms.com">
<EntityCreationDate xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="date"></EntityCreationDate>
<GC_ReferenceNo xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string"></GC_ReferenceNo>
<GC_Subject xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string"></GC_Subject>
<GC_StartDate xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="date"></GC_StartDate>
<GC_EndDate xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="date"></GC_EndDate>
<GC_FirmName xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string"></GC_FirmName>
<GC_HostingType xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string"></GC_HostingType>
<GC_Power xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="string"></GC_Power>
<GC_VMCOUNT xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="number"></GC_VMCOUNT>
</Memo>

@rakeshramesh

To update the values of Structured Document Tags (SDTs) that are already bound to a custom XML part in your Word document, you can follow the approach outlined in your code with some modifications. Here’s a detailed explanation of how to achieve this:

  1. Load the Document: Start by loading your Word document that contains the SDTs.

  2. Iterate Through SDTs: Loop through all the SDTs in the document. You can check the type of each SDT to determine how to update its content.

  3. Update Plain Text SDTs: For SDTs of type PlainText, you can remove existing children and append new text based on the values you want to set. Use the doc.getDescriptorValue(sdt.getTitle()) method to retrieve the new value for the SDT.

  4. Update Drop-Down List SDTs: For SDTs of type DropDownList, you can set the selected value by accessing the list items and using the setSelectedValue method.

  5. Save the Document: After making the necessary updates, save the document to reflect the changes.

Here’s a modified version of your code that incorporates these steps:

private byte[] updateContentControl(InputStream is, IDocument doc) throws Exception {
    if (is == null) return null;
    Document wordDoc = new Document(is);
    
    for (Object o : wordDoc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {
        StructuredDocumentTag sdt = (StructuredDocumentTag) o;
        switch (sdt.getSdtType()) {
            case SdtType.PLAIN_TEXT: {
                sdt.removeAllChildren();
                Paragraph para = sdt.appendChild(new Paragraph(wordDoc));
                Run run = new Run(wordDoc, doc.getDescriptorValue(sdt.getTitle()));
                para.appendChild(run);
                break;
            }
            case SdtType.DROP_DOWN_LIST: {
                SdtListItem listItem = new SdtListItem(doc.getDescriptorValue(sdt.getTitle()));
                sdt.getListItems().add(listItem);
                sdt.getListItems().setSelectedValue(listItem);
                break;
            }
            // Handle other SDT types as needed
        }
    }
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    wordDoc.save(baos, SaveFormat.DOCX);
    return baos.toByteArray();
}

Important Notes:

  • Ensure that the XML part is correctly set up and that the XPath expressions used in your SDTs match the structure of your XML data.
  • You can also refer to the Aspose documentation for more details on binding SDTs to custom XML parts and updating their content dynamically.

This approach will allow you to update the values in your SDTs at runtime effectively. If you have any further questions or need additional assistance, feel free to ask!

I am doing same but it throwing me error:
Cannot insert a node of this type at this location.

com.ser.blueline.BlueLineException: Cannot insert a node of this type at this location.
	at moro.GreenCertMemoCreation.createAndAttachMemo(GreenCertMemoCreation.java:69)
	at moro.GreenCertMemoCreation.execute(GreenCertMemoCreation.java:40)
	at de.ser.doxis4.agentserver.UnifiedAgent.execute(UnifiedAgent.java:254)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
	at junit.ExampleTests.testForGroovyAgentResult(ExampleTests.groovy:44)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:112)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:40)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:60)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:52)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
	at jdk.proxy1/jdk.proxy1.$Proxy2.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:119)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:66)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
Caused by: java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.
	at com.aspose.words.CompositeNode.zzZy(Unknown Source)
	at com.aspose.words.CompositeNode.insertAfter(Unknown Source)
	at com.aspose.words.CompositeNode.appendChild(Unknown Source)
	at moro.GreenCertMemoCreation.updateContentControl(GreenCertMemoCreation.java:87)
	at moro.GreenCertMemoCreation.createAndAttachMemo(GreenCertMemoCreation.java:62)
	... 51 more


Cannot insert a node of this type at this location.
java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.
	at com.aspose.words.CompositeNode.zzZy(Unknown Source)
	at com.aspose.words.CompositeNode.insertAfter(Unknown Source)
	at com.aspose.words.CompositeNode.appendChild(Unknown Source)
	at moro.GreenCertMemoCreation.updateContentControl(GreenCertMemoCreation.java:87)
	at moro.GreenCertMemoCreation.createAndAttachMemo(GreenCertMemoCreation.java:62)
	at moro.GreenCertMemoCreation.execute(GreenCertMemoCreation.java:40)
	at de.ser.doxis4.agentserver.UnifiedAgent.execute(UnifiedAgent.java:254)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
	at junit.ExampleTests.testForGroovyAgentResult(ExampleTests.groovy:44)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:112)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:40)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:60)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:52)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
	at jdk.proxy1/jdk.proxy1.$Proxy2.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:119)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:66)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

Green Certificate Memo Template (plain Text).docx (34.3 KB)

I have attached the word template here

@rakeshramesh Structured document tags can be placed on different levels of document structure. For example block level SDTs can contain block level nodes as children, inline levels SDTs can contain only inline level nodes. So you should modify your code like this:

case SdtType.PLAIN_TEXT: {
    sdt.removeAllChildren();
    Run run = new Run(doc, sdt.getTitle());
    if(sdt.getLevel() == MarkupLevel.BLOCK) {
        Paragraph para = sdt.appendChild(new Paragraph(doc));
        para.appendChild(run);
    }
    else if(sdt.getLevel() == MarkupLevel.INLINE)
    {
        sdt.appendChild(run);
    }
    break;
}