We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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);

}

}



<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0in; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

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”);<o:p></o:p>

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");

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)