Swap Content Control SDT

Hi,
I’m using Aspose Word for Java v11.
Is there a way to swap two or more content control SDT inside the same document.

Thanks in advance,
Ernesto.

@ernestocastellano Sure, you can use CompositeNode.InsertBefore or CompositeNode.InsertAfter methods to achieve this. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
// Get SDTs
StructuredDocumentTag sdt1 = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
StructuredDocumentTag sdt2 = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 1, true);
// Make sure SDTs are on the same level
if (sdt1.Level == sdt2.Level)
{
    // Clone original SDTs and puth clones after the original ones, 
    // these clones will be used as markers.
    Node clone1 = sdt1.Clone(false);
    Node clone2 = sdt2.Clone(false);

    sdt1.ParentNode.InsertAfter(clone1, sdt1);
    sdt2.ParentNode.InsertAfter(clone2, sdt2);

    // swap SDTs
    clone1.ParentNode.InsertAfter(sdt2, clone1);
    clone2.ParentNode.InsertAfter(sdt1, clone2);

    // Remove markers.
    clone1.Remove();
    clone2.Remove();
}

doc.Save(@"C:\Temp\out.docx");

In my case the SDTs are not on the same level. It’s however possible to swap SDTs on different levels?

Thanks,
Ernesto

@ernestocastellano I am afraid there is no direct way to swap SDTs, that are placed on different levels. For example inline level SDT can contain only inline nodes, such as Run or Shape such nodes cannot be placed on block level of the document, so SDT with such nodes also cannot be placed on block level.

Please see Aspose.Words Document Object model for more information:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

If SDTs are empty, it would be easier to simply recreate them on different levels.

In this case what you suggest to do? It’s better create others SDTs instead of swap them? If I create new SDTs how can copy the content of an existing SDTs (RICH TEXT type) in the new one?

Thanks in advance,

Ernesto.

@ernestocastellano The problem will be the same, since on different levels different types of nodes are allowed in SDT. For example, block level SDT contains paragraph and table and it is required to move it to inline level, in this case only inline nodes can be copied into the SDT on inline level, i.e. it is not possible to put paragraphs and tables into SDT on inline level. And vice versa, if your SDT is on inline level and contains Run node, it cannot be directly copied into SDT on block level, it is required to add a paragraph first and then copy Run into paragraph.

Ok, I understood. So how to create a new Content Control SDT before or after an existing one?

Thanks in advance.

@ernestocastellano The approach is similar as in the code above. You can use InsertBefore or InsertAfter methods:

Document doc = new Document(@"C:\Temp\in.docx");

// Get SDT
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
// Create SDT on the same level.
StructuredDocumentTag newSdt = new StructuredDocumentTag(doc, SdtType.RichText, sdt.Level);
// Insert newly create SDT after existing SDT.
sdt.ParentNode.InsertAfter(newSdt, sdt);

doc.Save(@"C:\temp\out.docx");