Skip to main content

How to Work with Rich Formatted Content

  • 4 minutes to read

The spreadsheet controls provide support for formatted cell content out-of-the-box via an in-place cell editor and a set of dedicated methods that the Spreadsheet text formatting service and cell objects provide.

Call the dxSpreadSheetTextService.IsRTFSupported class function to identify if the Formatted Text Service is available. If the function returns True, you can call any class method that the TdxSpreadSheetFormattedTextService class provides in addition to the end-user functionality available for an active in-place cell editor. End-user commands or key combination change the selected text or the editor’s current font.

Command Key Combination Text Formatting Action
ChangeFontColor No key combination. Applies the selected color.
ChangeFontName No key combination. Applies the selected font typeface.
DecreaseFontSize No key combination. Decreases the font size.
IncreaseFontSize No key combination. Increases the font size.
ToggleFontBold Ctrl+B (Ctrl+2) Toggles the “Bold” font attribute.
ToggleFontItalic Ctrl+I (Ctrl+3) Toggles the “Italic” font attribute.
ToggleFontStrikeout Ctrl+5 Toggles the “Strikeout” font attribute.
ToggleFontUnderline Ctrl+U (Ctrl+4) Toggles the “Underline” font attribute.
No command. Ctrl+Equals Sign (=) Toggles the “Subscript” font attribute.
No command. Ctrl+Shift+Equals Sign (=) Toggles the “Superscript” font attribute.

The GetAsRTF and SetAsRTF class functions are the principal public API members that the Formatted Text Service provides. You can call them to transfer RTF-formatted text strings between worksheet cells and other controls that can work with formatted text, such as the TdxRichEditControl.

A Formatted Text Example

The following code example copies the Rich Edit control’s formatted content to the focused cell.

var
  ATableView: TdxSpreadSheetTableView;
  ADocument: IdxRichEditDocument;  // A variable that provides access to the Rich Edit control's public API
  ACell: TdxSpreadSheetCell;
  AText: string;
//...
  ATableView := dxSpreadSheet1.ActiveSheetAsTable;
  ADocument := dxRichEditControl1.Document;
  ACell := ATableView.CreateCell(ATableView.Selection.FocusedRow, ATableView.Selection.FocusedColumn);  // Obtains the cell object that corresponds to the focused cell
  if(dxSpreadSheetTextService.IsRTFSupported) then  // If the text service provides support for RTF strings...
  begin
    AText := ADocument.GetRtfText;  // Obtains the Rich Edit control's content as an RTF string
    dxSpreadSheetTextService.SetAsRTF(ACell, AText);  // Assigns the obtained RTF string to the cell object that corresponds to the focused cell
  end
  else  // If the text service provides no support for RTF text...
  begin
    AText := ADocument.GetText;  // Obtains the Rich Edit control's content as a plain text
    ACell.SetText(AText);  // Assigns a plain text string to the focused cell
  end;

To perform the reverse operation and append the focused cell’s content with all its text attributes to a document opened in the Rich Edit control, refer to the following example.

var
  ATableView: TdxSpreadSheetTableView;
  ADocument: IdxRichEditDocument;  // A variable that provides access to the Rich Edit control's public API
  ACell: TdxSpreadSheetCell;
  AText: string;
//...
  ATableView := dxSpreadSheet1.ActiveSheetAsTable;
  ADocument := dxRichEditControl1.Document;
  AText := '';
  if(ATableView.Cells[ATableView.Selection.FocusedRow, ATableView.Selection.FocusedColumn] <> nil) then  // If the focused cell has a corresponding cell object...
  begin
    ACell := ATableView.Cells[ATableView.Selection.FocusedRow, ATableView.Selection.FocusedColumn];
    if(dxSpreadSheetTextService.IsRTFSupported) then // If the text service provides support for RTF strings...
      dxSpreadSheetTextService.GetAsRTF(ACell, AText);  // Copies an RTF formatted string from the cell object to the AText variable
    else  // If the text service provides no support for RTF text...
      AText := ACell.AsString;
  end;
  ADocument.AppendRtfText(AText, TdxRichEditInsertOptions.KeepSourceFormatting);  // Appends the obtained RTF string to the Rich Edit control's current content