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

RADFlowDocument Replace Text

4 Answers 881 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Khawar
Top achievements
Rank 1
Khawar asked on 21 Jan 2019, 09:31 PM

Hi,

I have an existing word document and I need to remove paragraph/sentence/line.

stream input = File.OpenRead(filepath);
RadFlowDocument document  = provider.Import(input);

RadFlowDocumentEditor  editor = new RadFlowDocumentEditor(document);     
editor.ReplaceText("<Expr2>", ltr.Expr2));   
editor.ReplaceText("<Expr3>", ltr.Expr3));

If ltr.Expr2 is blank, then it should remove blank lines from document where <Expr2> exists, otherwise replace text with ltr.Expr2 value. 

Any help would be greatly appreciated.

4 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 24 Jan 2019, 11:39 AM
Hi Khawar,

The ReplaceText() method replaces only the Run elements in the document and cannot delete a paragraph. To achieve this behavior, I would suggest you to manually remove the run and, according to the value of ltr.Expr2, delete the underlying paragraph or insert the value of the variable as the Text of the Run object:
foreach (var run in this.document.EnumerateChildrenOfType<Run>().ToList())
{
    if (run.Text == "<Expr2>")
    {
        if (string.IsNullOrEmpty(ltr.Expr2))
        {
            var section = run.Paragraph.Parent as Section;
            section.Blocks.Remove(run.Paragraph);
        }
        else
        {
            run.Text = ltr.Expr2;
        }
    }
}


Regards,
Tanya
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.
0
Khawar
Top achievements
Rank 1
answered on 24 Jan 2019, 04:02 PM
It worked!!Thank you so much.
0
Khawar
Top achievements
Rank 1
answered on 05 Dec 2019, 05:57 PM

Hi Admins,

I have a question regarding formatted text to insert in a document using RadFlowDocument editor.

I m using  this line of code  

editor.ReplaceText("<LetterFieldInsert>", initltr.LetterFieldInsert)

problem is, sometimes text can have line feed (i.e. CrLf or Lf) in it and I want to show it with formatting if there is any. 

Currently, RadFlowDocument editor isn't recognizing line feed and lumped all together or in other words, wrapped in a single sentence.

Would you please advise, how can I fix this issue? 

Your help is greatly appreciated.

Thanks

 

0
Tanya
Telerik team
answered on 10 Dec 2019, 09:35 AM

Hi Khawar,

With the current implementation of the ReplaceText() method, only plain text can be inserted. The content passed as a parameter to the method is stripped from the formatting and line feeds as it needs to be added as content to a single Run element. We have a request to provide you with the ability to replace with different elements and you can vote for its implementation using the related public item: WordsProcessing: Introduce a way to replace text with other document elements. You can also follow this item if you would like to receive updates about status changes on it.

Regards,
Tanya
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
WordsProcessing
Asked by
Khawar
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Khawar
Top achievements
Rank 1
Share this question
or