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

RadDocument SelectAll not Working for Merge

4 Answers 169 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
AP
Top achievements
Rank 1
Iron
Iron
Veteran
AP asked on 09 May 2011, 10:28 AM
I'm trying to merge several files into one document, in order to produce a dynamic report.

I can load data into a RadRichTextBox, however after using the code in another forum post, to load another file into a RadDocument object, and then use the Selection.SelectAll method to insert a fragment into the RichTextBox.

private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
  
                RadDocument document2 = new RadDocument();
  
                document2 = ImportRTF();
                document2.Selection.SelectAll(); 
                this.radRichTextBox1.Document.InsertFragment(document2.Selection.CopySelectedDocumentElements());
                }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

The ImportRTF function is:- (and works fine, as it loads a file into the RichTextBox perfectly)
public RadDocument ImportRTF()
{
    try
    {
        RadDocument document = null;
        IDocumentFormatProvider provider = new RtfFormatProvider();
        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.Filter = "Documents|*.rtf";
        openDialog.Multiselect = false;
        bool? dialogResult = openDialog.ShowDialog();
        if (dialogResult == true)
        {
            //MessageBox.Show(openDialog.File.FullName);
            using (FileStream stream = openDialog.File.OpenRead())
            {
                document = provider.Import(stream);
            }
        }
        return document;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
         
        return null;
    }
}

However, when calling the SelectAll function on a RadDocument (which isn't a RichTextBox contents) , it doesn't return anything (IsEmpty is True).

Modifying the code to look at the existing RichTextBox contents, and the selection code works fine.

Please help, as it's crucial I can combine document files together.

Thanks

4 Answers, 1 is accepted

Sort by
0
AP
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 09 May 2011, 04:07 PM
Also, even when working around the issue with an unsatifactory bodge, (by loading the second document into a RadRichTextBox, and calling the UpdateEditorLayout method, and then doing a SelectAll on the RichTextBox's Document), I can't get the fragment to insert at the end of the document.

I have tried using :-
radRichTextBox1.Document.CaretPosition.MoveToLastPositionInDocument();

and also :-

DocumentPosition position = new DocumentPosition(this.radRichTextBox1.Document);
position.MoveToLastPositionInDocument();
radRichTextBox1.Document.InsertFragment(fragment,position);

And neither works, the insertion takes place where the caret is on the UI, regardless of what code-behind does.
0
Mike
Telerik team
answered on 11 May 2011, 03:53 PM
Hello AP,

Here how you should modify the code in order to achieve what you want:

private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RadDocument document2 = new RadDocument();
 
                document2 = ImportRTF();
 
                //We have to do the following before working with the API of RadDocument
                document2.Measure(RadDocument.MAX_DOCUMENT_SIZE);
                document2.Arrange(new RectangleF(PointF.Empty, document2.DesiredSize));
                document2.Selection.SelectAll();
 
                using (var previousPos = new DocumentPosition(document2.CaretPosition, true))
                {
                    this.radRichTextBox1.Document.CaretPosition.MoveToLastPositionInDocument();
                    //Unfortunately there is problem with the other overload of the method so we have to workaround
                    this.radRichTextBox1.Document.InsertFragment(document2.Selection.CopySelectedDocumentElements());
                    this.radRichTextBox1.Document.CaretPosition.MoveToPosition(previousPos);
                }
 
                this.radRichTextBox1.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Let us know if this works for you.

Best wishes,
Mike
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Shrenik
Top achievements
Rank 1
answered on 29 Dec 2011, 12:08 AM
Hi,

We are evaluting RadRichTextBox Control for usage in our project. We are currently using 2011.3.1116.1040 version. Issue is when we try to merge documents, formating is not getting preserved. Font and Size of fonts are rollback to Verdana. Any help?

Thanks,
Shrenik
0
Boby
Telerik team
answered on 30 Dec 2011, 04:55 PM
Hello Shrenik,
You can use the new DocumentFragment constructor:
// you can use other format providers as well
XamlFormatProvider provider = new XamlFormatProvider();
 
List<RadDocument> documents = new List<RadDocument>()
{
    // this will create some test documents that will be merged
    provider.Import(xaml1),
    provider.Import(xaml2),
};
 
RadDocument mergedDocument = new RadDocument();
foreach (var document in documents)
{
    mergedDocument.CaretPosition.MoveToLastPositionInDocument();
    mergedDocument.InsertFragment(new DocumentFragment(document));
}
 
// set the document to RadRichTextBox
this.radRichTextBox.Document = mergedDocument;
If this doesn't help, could you please specify what is the format of the documents (HTML, docx, XAML), or even send it to us so we can investigate the issue further?

Regards,
Boby
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Mike
Telerik team
Shrenik
Top achievements
Rank 1
Boby
Telerik team
Share this question
or