This is a migrated thread and some comments may be shown as answers.

Edit RadRichTextBox content

2 Answers 215 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Uma
Top achievements
Rank 1
Uma asked on 12 Aug 2019, 11:05 AM

Hi Team,

 

I want to change the RadRichTextBox content(remove some invalid characters) in LostFocus event. Initially, i have loaded the RadRichTextBox with some header and footer content. Then, i am entering some data(Paste data also) and tabout. While losing the focus, need to remove some invalid character and save in the same document. I have tried like below. 

 

           Telerik.Windows.Documents.FormatProviders.Txt.TxtFormatProvider provider = new Telerik.Windows.Documents.FormatProviders.Txt.TxtFormatProvider();
           string output = provider.Export(richtextbox.Document);   // getting textbox's text     
           string text = output;
                foreach (char letter in text)
                {                  
                            if (//Condition)
                            {  
                              // here remove some invalid characters based on some condition.
                                output = output.Remove(index, 1);                                
                            }                
                }                                      
              RadDocument document = new RadDocument();
              document = provider.Import(output);         

              radRichTextBox.Document = document;  // update the document .. It is an error

              object Docx = docprovider.Export(document); // DocxFormatProvider

 

After removing the some content, document's header and footer also removed. Can you please suggest how to edit the content of RadRichTextBox ?

 

2 Answers, 1 is accepted

Sort by
0
Uma
Top achievements
Rank 1
answered on 13 Aug 2019, 11:24 AM
Any Update?
0
Martin
Telerik team
answered on 15 Aug 2019, 08:25 AM
Hello Uma,

The reason for the removing of the header and the footer sections is the exporting of the document as plain text. The TxtFormatProvider keeps only the text content of the document and ignores most of the additional document data because they are not supported in plain text.

My suggestion is to store the header and the footer before you make the document changes. At first you will need to get the header/footer:
Header header = radDocument.EnumerateChildrenOfType<Section>().First().Headers.Default;

 and when you are ready with all the document text changes, you can set a new header/footer with the stored content:
this.radRichTextBox.UpdateHeader(this.radRichTextBox.Document.Sections.First, HeaderFooterType.Default, header);

For more information about the usage of headers and footers you can check the Header and Footer help section.

Hope this is helpful.

Regards,
Martin
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
RichTextBox
Asked by
Uma
Top achievements
Rank 1
Answers by
Uma
Top achievements
Rank 1
Martin
Telerik team
Share this question
or