Back to Devexpress

How to Work with Rich Formatted Content

vcl-168008-expressspreadsheet-how-to-work-with-rich-formatted-content.md

latest9.1 KB
Original Source

How to Work with Rich Formatted Content

  • Jun 21, 2022
  • 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.

CommandKey CombinationText Formatting Action
ChangeFontColorNo key combination.Applies the selected color.
ChangeFontNameNo key combination.Applies the selected font typeface.
DecreaseFontSizeNo key combination.Decreases the font size.
IncreaseFontSizeNo key combination.Increases the font size.
ToggleFontBoldCtrl+B (Ctrl+2)Toggles the “Bold” font attribute.
ToggleFontItalicCtrl+I (Ctrl+3)Toggles the “Italic” font attribute.
ToggleFontStrikeoutCtrl+5Toggles the “Strikeout” font attribute.
ToggleFontUnderlineCtrl+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.

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

delphi
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;
cpp
TdxSpreadSheetTableView *ATableView;
  IdxRichEditDocument *ADocument; // A pointer to the interface providing access to the Rich Edit control's public API
  TdxSpreadSheetCell *ACell;
  UnicodeString AText;
//...
  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->InheritsFrom(TdxSpreadSheetFormattedTextService)) // If the text service provides support for RTF text...
  {
    AFormattedValue = ADocument->GetRtfText(); // Obtains the Rich Edit control's content as an RTF string
    TdxSpreadSheetFormattedTextService::SetAsRTF(ACell, AText); // Assigns the obtained RTF string to the cell object that corresponds to the focused cell
  }
  else // If the text service provides no support for RTF text...
  {
    AText = ADocument->GetText(); // Obtains the Rich Edit control's content as a plain text string
    ACell->SetText(AText); // Assigns a plain text string to the cell object
  }

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.

delphi
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
cpp
TdxSpreadSheetTableView *ATableView;
  IdxRichEditDocument *ADocument; // A pointer to the interface that provides access to the Rich Edit control's public API
  TdxSpreadSheetCell *ACell;
  UnicodeString AText;
//...
  ATableView = dxSpreadSheet1->ActiveSheetAsTable;
  ADocument = dxRichEditControl1->Document;
  AText = "";
  if(ATableView->Cells[ATableView->Selection->FocusedRow][ATableView->Selection->FocusedColumn] != NULL) // If the focused cell has a corresponding cell object
  {
    ACell = ATableView->Cells[ATableView->Selection->FocusedRow][ATableView->Selection->FocusedColumn];
    if(dxSpreadSheetTextService->InheritsFrom(TdxSpreadSheetFormattedTextService)) // If the text service provides support for RTF text...
      TdxSpreadSheetFormattedTextService::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;
  }
  ADocument->AppendRtfText(AText, TdxRichEditInsertOptions::KeepSourceFormatting); // Appends the obtained RTF string to the Rich Edit control's current content