Table of Contents

Rich Editor RTF IO Guide

TTMSFNCRichEditorRTFIO is a non-visual Windows component that lets TTMSFNCRichEditor read and write RTF files. Use it when you need to exchange documents with Microsoft Word, WordPad, or any RTF-capable application. For cross-platform scenarios, use the native .RTE format via TTMSFNCRichEditor.SaveToFile/LoadFromFile instead.

The component exposes one published property — RichEditor — and two method pairs: file-path overloads for simple cases, and stream overloads for database blobs or HTTP transfers.

Note

TTMSFNCRichEditorRTFIO is Windows-only (Win32/Win64). The platform attribute pidWin32 or pidWin64 is set at registration; the component is not available on macOS, iOS, Android, or TMS WEB Core.

Connecting to an editor

Drop a TTMSFNCRichEditorRTFIO onto the same form as a TTMSFNCRichEditor. The constructor scans the form owner for the first TTMSFNCRichEditor instance and wires RichEditor automatically. If the form has more than one editor, or if you create the component at runtime, assign RichEditor yourself before calling Load or Save:

TMSFNCRichEditorRTFIO1.RichEditor := TMSFNCRichEditor2;

Calling Load or Save when RichEditor is nil raises an exception.

File-based Load and Save

The simplest workflow is file-based. Call Save(filename) to write the current editor content as RTF, and Load(filename) to replace the editor content from an RTF file.

// TTMSFNCRichEditorRTFIO is a non-visual component.
// Drop it on the form, connect it to a TTMSFNCRichEditor, then call Load/Save.

procedure TForm1.ButtonSaveClick(Sender: TObject);
begin
  // Writes the editor content as RTF to disk
  TMSFNCRichEditorRTFIO1.Save('document.rtf');
end;

procedure TForm1.ButtonLoadClick(Sender: TObject);
begin
  // Reads RTF from disk and populates the connected editor
  TMSFNCRichEditorRTFIO1.Load('document.rtf');
end;

Stream-based Load and Save

Stream-based IO lets you pass RTF content through database blobs or in-memory buffers without writing to disk. Both Save and Load accept a TStream parameter.

// Saving to and loading from a TMemoryStream lets you transfer RTF
// content through a network socket or database blob without touching the file system.

procedure TForm1.ButtonSaveStreamClick(Sender: TObject);
var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    TMSFNCRichEditorRTFIO1.Save(ms);
    // ms now contains the RTF bytes; send or store as needed
    ms.SaveToFile('buffer.rtf');
  finally
    ms.Free;
  end;
end;

procedure TForm1.ButtonLoadStreamClick(Sender: TObject);
var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    ms.LoadFromFile('buffer.rtf');
    ms.Position := 0;
    TMSFNCRichEditorRTFIO1.Load(ms);
  finally
    ms.Free;
  end;
end;

The Load(AStream) overload resets the stream position to 0 internally before reading, so you do not need to reset it yourself.

Runtime creation and wiring

When the editor and IO component need to be created programmatically, instantiate TTMSFNCRichEditorRTFIO, assign RichEditor, and then call Load.

// Creating and wiring TTMSFNCRichEditorRTFIO at runtime, then round-tripping content
procedure TForm1.FormCreate(Sender: TObject);
var
  rtfio: TTMSFNCRichEditorRTFIO;
  ms: TMemoryStream;
begin
  rtfio := TTMSFNCRichEditorRTFIO.Create(Self);
  rtfio.RichEditor := TMSFNCRichEditor1;

  // Pre-load a template document
  ms := TMemoryStream.Create;
  try
    ms.LoadFromFile('template.rtf');
    ms.Position := 0;
    rtfio.Load(ms);
  finally
    ms.Free;
  end;
end;

Combining Load, programmatic editing, and stream Save

This example loads a template from disk, appends a timestamped line via the editor API, then streams the result to a TMemoryStream — combining file Load, the rich editor document API, and stream Save in a single workflow:

procedure TForm1.ButtonProcessClick(Sender: TObject);
var
  ms: TMemoryStream;
begin
  // Load the template RTF from disk into the editor
  TMSFNCRichEditorRTFIO1.Load('template.rtf');

  // Append a timestamped line via the editor API
  TMSFNCRichEditor1.AddLineBreak;
  TMSFNCRichEditor1.AddText('Generated: ' + DateTimeToStr(Now));

  // Stream Save the modified document (e.g. for database blob storage)
  ms := TMemoryStream.Create;
  try
    TMSFNCRichEditorRTFIO1.Save(ms);
    ms.SaveToFile('output.rtf');
  finally
    ms.Free;
  end;
end;

Common pitfalls

  • RichEditor not assigned. Calling Load or Save before assigning RichEditor raises an exception. On a form with a single TTMSFNCRichEditor, design-time drop handles this automatically. For runtime creation, assign explicitly.
  • Windows only. Do not add TTMSFNCRichEditorRTFIO to a cross-platform form that targets mobile or web; the unit will not compile. Use the native .RTE format for cross-platform persistence.
  • RTF fidelity. Import converts RTF to the editor's internal element model; some advanced RTF features (tables, complex list styles) may not round-trip perfectly. Verify critical documents in both directions.
  • File encoding. Load(const FileName) reads the file using ANSI encoding (Delphi XE2+). RTF files produced by modern editors are typically ANSI-compatible for ASCII content; Unicode characters are escaped by standard RTF producers.

See also