Clone a paragraph
Hi,
I'm trying to duplicate a paragraph and all its content in the SfRichTextBoxAdv.
The current code looks like that :
ParagraphAdv? currentParagraph = _textBox.Selection.Start.Paragraph;
if (currentParagraph == null)
{
return;
}
ParagraphAdv newParagraph = new();
Node[] nodesCopy = new Node[currentParagraph.Inlines.Count];
currentParagraph.Inlines.CopyTo(nodesCopy, 0);
foreach (Node node in nodesCopy)
{
newParagraph.Inlines.Add(node);
}
if (currentParagraph.Owner is SectionAdv sec)
{
sec.Blocks.Insert(sec.Blocks.IndexOf(currentParagraph)+1,newParagraph);
}
The problem that I'm facing is that the nodes are not really cloned with the CopyTo method.
The result is that the paragraph is not cloned, the nodes are just moving to the new paragraph and the old one is empty after operation.
Any idea on how to proceed ?
Thank you.
Se Bo. When you need to clone an existing paragraph and its inlines, create a new instance for the paragraph and add the existing inlines to the new paragraph by creating new instances of the inlines.
If you iterate over the existing inline collection and add them to the new paragraph, they will be removed from the existing paragraph, as inlines are objects. We have created a sample application to achieve this requirement, which is attached below.
Attachment: SfRichTextBoxAdv_Paragraph_Clone_e4eaa115.zip
Thank you. I had this idea but hoped it would be managed by the framework.
I saw a Clone method in various objects in your plateform but they are Internal or protected.
I think many methods should be public because they can be very useful ;)
Thanks again.
Se Bo, the SfRichTextBoxAdv is a UI editor control used to perform actions on documents through the UI. Since your function involves cloning paragraphs for document processing, you can iterate through the document elements explicitly and perform the necessary manipulations using the .NET Word Library (DocIO). After that, you can load the modified document into the SfRichTextBoxAdv. Please use the following code to clone a paragraph using DocIO and load it into SfRichTextBoxAdv.
|
WordDocument
document = new WordDocument(@"Assets/Sample.docx"); //Get the paragraph from the document to clone WParagraph para =
document.Sections[0].Body.Paragraphs[0] as WParagraph; //Clone the paragraph WParagraph
clonedPara = para.Clone() as
WParagraph; //Insert the cloned paragraph to the document WTextBody textBody = document.LastSection.ChildEntities[0] as WTextBody;
textBody.ChildEntities.Add(clonedPara); //open the document in SfRichTextBoxAdv richTextBoxAdv.Load(document); |
Please use the following UG link to learn more about DocIO:
https://help.syncfusion.com/file-formats/docio/overview