RichText Content Controls

Hi,
Im having problems setting text in RichText Content Controls, I cant get it to work.

I have a loop where i loop through all content controls in a document where i should set/change the text in that control. The code below works fine for planeText but not sure how to implement it for RichText, i can add a new richtext last in the document but its not really good enought for what im trying to achine, I really need to set the text in a specific CC RichText and not just add a new one… Anyone pls?

foreach (StructuredDocumentTag sdt in sdtNodes)
{
    transText = GetText();
    if (transText != "")
    {
        if (sdt.SdtType == SdtType.PlainText)
        {
            sdt.RemoveAllChildren();
            Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph;
            Run run = new Run(doc, transText);
            para.AppendChild(run);
        }
        else if (sdt.SdtType == SdtType.RichText)
        {
            // ``??? 
        }
    }
}

br Martin

Hi Martin,

Thanks for your inquiry. Please use the StructuredDocumentTag.AppendChild method to add the specified node to the end of the list of child nodes for this node. See the following highlighted code snippet. Hope this helps you.

If you still face problem, please share your input document here for testing. I will investigate the issue and provide you more information about your query.

foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    if (sdt.SdtType == SdtType.RichText)
    {
        sdt.RemoveAllChildren();
        Paragraph para = new Paragraph(doc);
        para.AppendChild(new Run(doc, "Some text"));
        sdt.AppendChild(para);
    }
}

Hi,

I just tried the above mentioned piece of code and still get the ‘You cannot insert this type of node here’ exception on the line marked in yellow.

The file used is added as an attachment.
I’ve tried it with .NET Words 16.2

The code:

static void Main(string[] args)
{
    string input = @"TestInput.docx";
    string output = @"TestOutput.docx";

    Document doc = new Document(input);
    foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
    {
        if (sdt.Tag != "RichContentControl")
            continue;

        sdt.RemoveAllChildren();
        Paragraph para = new Paragraph(doc);
        para.AppendChild(new Run(doc, "some text"));
        sdt.AppendChild(para);
    }
    doc.Save(output);
}

Hi Martin,

Thanks for your inquiry. You can use following modified code example to update content control. This code works if content controls are not bound to CustomXML.

Your document contains CustomXML for contents control. I am afraid, currently there is no way to bind content control to CustomXML. Your request has been linked to the appropriate issue (WORDSNET-4738) and you will be notified as soon as API to bind content control to CustomXML properties is available. Sorry for the inconvenience.

Document doc = new Document(MyDir + "TestInput.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    if (sdt.SdtType == SdtType.RichText)
    {
        if (sdt.Tag != "RichContentControl")
            continue;
        sdt.RemoveAllChildren();
        Paragraph para = new Paragraph(doc);
        para.AppendChild(new Run(doc, "New Rich text"));
        sdt.AppendChild(para);
    }
    else if (sdt.SdtType == SdtType.PlainText)
    {
        Paragraph paragraph = (Paragraph)sdt.GetChild(NodeType.Paragraph, 0, true);
        if (paragraph != null)
        {
            paragraph.RemoveAllChildren();
            paragraph.AppendChild(new Run(doc, "**New Rich text"));
        }
        else
        {
            sdt.RemoveAllChildren();
            sdt.AppendChild(new Run(doc, "**New plain text"));
        }
    }
}
doc.Save(MyDir + @"Out v16.10.0.docx");
1 Like

The issues you have found earlier (filed as WORDSNET-4738) have been fixed in this Aspose.Words for .NET 17.5 update and this Aspose.Words for Java 17.5 update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(6)