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

How to replace a textplaceholder with multiparagraph text and set its format

1 Answer 103 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Tri
Top achievements
Rank 1
Tri asked on 05 Apr 2019, 11:51 AM

Hi All

I try to produce a generated document using RadDocument. Let's say I have template like this:

       A.  Chapter a

            <texttoreplace>

       B.  Chapter b

Chapter A, B and so on is a 'report template', each header is indented 150 point. I have multi paragraph text like this (2 paragraph in this sample below):

text1 text1

text2 text 2

All of these text is *not left  indented*. What I need is , I can produce  report like this:

       A.  Chapter a
            text1 text1

            text2 text2
       B.  Chapter b

I expect the second paragrah, "text2 text2" is also indented just look like first one (text1 text1).

I have tried to use this code to do this job:

01.foreach (var textRange in search.FindAll("<<toreplace>>"))
02.            {
03.// select text to replace
04.                docx.Selection.AddSelectionStart(textRange.StartPosition);
05.                docx.Selection.AddSelectionEnd(textRange.EndPosition);
06.                docxEdit = new Telerik.WinForms.Documents.Model.RadDocumentEditor(docx);
07.//remove the text
08.                docxEdit.Delete(false);
09.          //put 'multi paragraph' replacement text
10.                docxEdit.InsertFragment(fragment);
11.//just in case there more than one text to replace, loop
12.                docx.Selection.Clear();
13.                textRange.StartPosition.Dispose();
14.                textRange.EndPosition.Dispose();
15.            }

 

this code replace all occurrences of <<texttoreplace>> successfully, but the output is always like this:

       A.  Chapter a
            text1 text1
text2 text2
       B.  Chapter b

The second paragraph of the replacement text is not following destination paragraph indentation but use its own indent. 

Can I have any clue how to produce report I need ?

1 Answer, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 10 Apr 2019, 09:18 AM
Hello Tri,

When inserting a fragment inside a document, the formatting of the fragment is preserved leading to ignoring the current style which sets the indent of the paragraphs. What I can suggest in this case is to iterate through the paragraphs of the document inside the fragment and insert them so the current editing style can be preserved:
RadDocument documentToInsert = new RadDocument();
RadDocumentEditor editor = new RadDocumentEditor(documentToInsert);
documentToInsert.Sections.Add(new Section());
documentToInsert.Sections.First.Blocks.Add(new Paragraph());
editor.InsertLine("text1 text1");
editor.Insert("text2 text2");
 
foreach (var textRange in search.FindAll("<texttoreplace>"))
{
    // select text to replace
    this.radRichTextBox.Document.Selection.AddSelectionStart(textRange.StartPosition);
    this.radRichTextBox.Document.Selection.AddSelectionEnd(textRange.EndPosition);
    var docxEdit = new RadDocumentEditor(this.radRichTextBox.Document);
    //remove the text
    docxEdit.Delete(false);
    //put 'multi paragraph' replacement text
    foreach (var paragraph in documentToInsert.EnumerateChildrenOfType<Paragraph>())
    {
        foreach (var inline in paragraph.Inlines)
        {
            docxEdit.InsertInline(inline);
        }
        docxEdit.InsertParagraph();
    }
    //just in case there more than one text to replace, loop
    this.radRichTextBox.Document.Selection.Clear();
    textRange.StartPosition.Dispose();
    textRange.EndPosition.Dispose();
}

Hope this is helpful.

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
RichTextEditor
Asked by
Tri
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or