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

RadDocumentEditor - Insert table with HorizontalAlignment.Center

3 Answers 229 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 11 Mar 2016, 12:32 PM

Hello,
I have a RadDocument and want to exchange a specific text against a table with the same HorizontalAlignment and other style properties. i.e.:

some text some text some text some text
some text some text some text some text
             "centered bold text"
some text some text some text some text
some text some text some text some text

(the text "centered bold text" have to exchange against my table withHorizontalAlignment = Center and FontWeight = Bold)

TextRange rt = search.FindAll("centered bold text").First();
document.CaretPosition.MoveToPosition(rt.StartPosition);
document.Selection.AddSelectionStart(rt.StartPosition);
document.Selection.AddSelectionEnd(rt.EndPosition);

How can I get information about the style settings of the text at the CaretPosition?

The insertion of the table isn't without styles works:

RadDocumentEditor editor = new RadDocumentEditor(document);
// How to get the styles
editor.Delete(false);
Table table = GetSomeTelerikTable();
// How to set the style - especially Horizontal alignment for center/right
// table.HorizontalAlignment = RadHorizontalAlignment.Center;
editor.InsertTable(table);
...
// I tried the following for a fixed value of HorizontalAlignment, but this didn't work:
//editor.ChangeTableHorizontalAlignment(RadHorizontalAlignment.Center);
...
document.MeasureAndArrangeInDefaultSize();

How can I set this style properties for my new table?

Thanks a lot

 

3 Answers, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 15 Mar 2016, 12:06 PM
Hello Thomas,

If you would like to change a style property of paragraph or span you are on the correct track. This can be done by selecting the required text like you are doing it and applying the correct command.

The purpose of the ChangeTableHorizontalAlignment method that you have used is to align the whole table on the page. This could be useful only if the table width is smaller than the page width. Please have in mind that the table alignment does not affect the alignment of the text.

If you would like to change the alignment of the text the suitable approach is to call the ChangeTextAlignment method.
And if you would like to change the font-weight the method is ChangeFontWeight. Here is example of how this could be done:
DocumentTextSearch documentTextSearch = new DocumentTextSearch(this.radRichTextBox.Document);
 
TextRange textRange = documentTextSearch.FindAll("centered bold text").First();
 
this.radRichTextBox.Document.Selection.Clear();
this.radRichTextBox.Document.Selection.AddSelectionStart(textRange.StartPosition);
this.radRichTextBox.Document.Selection.AddSelectionEnd(textRange.EndPosition);
this.radRichTextBox.ChangeTextAlignment(Telerik.Windows.Documents.Layout.RadTextAlignment.Center);
this.radRichTextBox.ChangeFontWeight(FontWeights.Bold);



To apply the text formatting over a table you can select the table from its start position to its end position and change the required styling. Here is example of how this could be done:
{
    Table table = this.radRichTextBox.Document.EnumerateChildrenOfType<Table>().First();
 
    DocumentPosition start = new DocumentPosition(this.radRichTextBox.Document, false);
    start.MoveToStartOfDocumentElement(table);
 
    DocumentPosition end = new DocumentPosition(this.radRichTextBox.Document, false);
    end.MoveToEndOfDocumentElement(table);
 
 
    this.radRichTextBox.Document.Selection.Clear();
    this.radRichTextBox.Document.Selection.AddSelectionStart(start);
    this.radRichTextBox.Document.Selection.AddSelectionEnd(end);
 
    this.radRichTextBox.ChangeTextAlignment(Telerik.Windows.Documents.Layout.RadTextAlignment.Center);
    this.radRichTextBox.ChangeFontWeight(FontWeights.Bold);

You could also use the caret position in order to obtain any required document element if the position is placed in that element. Here is example of how this could be done:
DocumentPosition caretPosition = this.radRichTextBox.Document.CaretPosition;
 
SpanLayoutBox spanLayoutBox = caretPosition.GetCurrentSpanBox();
if (spanLayoutBox != null)
{
    Span span = spanLayoutBox.AssociatedSpan;
}
 
ParagraphLayoutBox paragraphLayoutBox = caretPosition.GetCurrentParagraphBox();
if (paragraphLayoutBox != null)
{
    Paragraph paragraph = caretPosition.GetCurrentParagraphBox().AssociatedParagraph;
}
 
if (caretPosition.IsPositionInsideTable)
{
    Table table = caretPosition.GetCurrentTableBox().AssociatedTable;
}



I hope this information answers your questions.

Regards,
Mihail
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Thomas
Top achievements
Rank 1
answered on 15 Mar 2016, 01:33 PM

Thanks for your reply,
Ok nice try - but it isn't working correctly. (I really want to set the alignment of the table not of the content within the cells)

The problem is that I haven't a rendered instance of the RadRichTextBox, so I tried the following:

RadDocumentEditor editor = new RadDocumentEditor(document);
...
editor.Delete(false);
...
table.Tag = "fdhlfdjksljhfalskjdfhlaksjd";
table.HorizontalAlignment = RadHorizontalAlignment.Center;
 
//RichTextBox
RadRichTextBox rtb = new RadRichTextBox();
rtb.Document = document;
Table t = rtb.Document.EnumerateChildrenOfType<Table>()
         .FirstOrDefault(val => val.Tag == "fdhlfdjksljhfalskjdfhlaksjd");
if (t != null)
{
    DocumentPosition start = new DocumentPosition(rtb.Document, false);
    start.MoveToStartOfDocumentElement(t);
    DocumentPosition end = new DocumentPosition(rtb.Document, false);
    end.MoveToEndOfDocumentElement(t);
    rtb.Document.Selection.Clear();
    rtb.Document.Selection.AddSelectionStart(start);
    rtb.Document.Selection.AddSelectionEnd(end);
    rtb.ChangeTableHorizontal(AlignmentRadHorizontalAlignment.Center);   
    rtb.ChangeFontWeight(FontWeights.Bold);
 
}

 

The content of the whole table is bold but the table isn't at the center of the Document - it stay left.

 

 

 

0
Mihail
Telerik team
answered on 17 Mar 2016, 09:14 AM
Hello Thomas,

I have just tested the table horizontal alignment with the code from your last post and everything is working as expected.

Could you please provide a simple demo application that reproduces the issue to a support ticket? This way we will be able to investigate the problem.

Regards,
Mihail
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
RichTextBox
Asked by
Thomas
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Thomas
Top achievements
Rank 1
Share this question
or