Want to update a new structuredocumenttag

i have certain sdt controls in my document… i want to update them (convert it into ricktextbox from plaintext)… or delete that control and insert a new of same name at same location…the location can be any inside a table , paragrph or whatever…

@Rubal

To update a Structured Document Tag (SDT) in your document, you can either convert an existing SDT control from plain text to rich text or remove the existing control and insert a new one with the same name at the desired location. Here’s how you can achieve both tasks using Aspose.Words for .NET.

Converting Plain Text SDT to Rich Text SDT

If you want to convert an existing plain text SDT to a rich text SDT, you can follow these steps:

  1. Retrieve the existing SDT from the document.
  2. Create a new rich text SDT.
  3. Copy the properties and content from the plain text SDT to the new rich text SDT.
  4. Replace the old SDT with the new one.

Here’s a code example demonstrating this:

Document doc = new Document("YourDocument.docx");
NodeCollection sdtCollection = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);

foreach (StructuredDocumentTag sdt in sdtCollection)
{
    if (sdt.SdtType == SdtType.PlainText)
    {
        // Create a new rich text SDT
        StructuredDocumentTag richTextSdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
        richTextSdt.Title = sdt.Title; // Copy title
        richTextSdt.Tag = sdt.Tag; // Copy tag
        richTextSdt.SetText(sdt.GetText()); // Copy content

        // Replace the old SDT with the new one
        sdt.ParentNode.ReplaceChild(richTextSdt, sdt);
    }
}

doc.Save("UpdatedDocument.docx");

Deleting and Inserting a New SDT

If you prefer to delete the existing SDT and insert a new one with the same name, you can do the following:

  1. Find the existing SDT and remove it.
  2. Create a new SDT with the same properties and insert it at the desired location.

Here’s how you can implement this:

Document doc = new Document("YourDocument.docx");
NodeCollection sdtCollection = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);

foreach (StructuredDocumentTag sdt in sdtCollection)
{
    if (sdt.Tag == "YourSDTTag") // Check for the specific tag
    {
        // Remove the existing SDT
        sdt.RemoveSelfOnly();

        // Create a new SDT
        StructuredDocumentTag newSdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline)
        {
            Title = "Your Title",
            Tag = "YourSDTTag"
        };

        // Insert the new SDT at the same location
        sdt.ParentNode.InsertAfter(newSdt, sdt);
    }
}

doc.Save("UpdatedDocument.docx");

Conclusion

By following the above examples, you can effectively update your SDT controls in the document, either by converting them or by replacing them with new ones. Make sure to adjust the properties as needed to fit your specific requirements.

For more detailed information, you can refer to the Aspose.Words documentation on Structured Document Tags.

@Rubal Please see our documentation to learn how to work with SDTs using Aspose.Words:
https://docs.aspose.com/words/net/working-with-content-control-sdt/

foreach (var sdt in matchingSdts)
{
    StructuredDocumentTag newSdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline)
    {
        Title = "SC_CF_"+sdt.Tag.Split('.').Last(),
        Tag = "SC_CF_" + sdt.Tag.Split('.').Last(),
        IsShowingPlaceholderText=true
    };
                      

    // Insert the new SDT at the same location
    sdt.ParentNode.InsertAfter(newSdt, sdt);
    sdt.Remove();
}

everything is working fine …but new sdt is coming up with some text nature of property…i want blank control

@Rubal You should simply remove all existing nodes in the newly created sdt.