How to create a RTF file from Delphi / C++Builder / Lazarus application?

A frequent question is how to send a document from a Delphi application to RTF. It is a popular format and if you don’t have Microsoft Word installed on your computer you can use WordPad to open the file. This format is supported by Mac OS X’s default editor TextEdit. Most text editors support RTF in one form or another.

Rich Text Format (RTF) is an advanced text format developed by Microsoft in 1982. RTF is often used to create e-books, less commonly – documents. Initially, the purpose of this format was to create a file convenient to work with books and documents. This was done through built-in meta tags that allow you to quickly navigate through the document.

This format Is quite accessible. Moreover, Windows usually contains a DLL for rendering and displaying RTF.

Three typical ways how to save in RTF format from Delphi and Lazarus

There are so many tools to save text in RTF from Delphi application.
1. You can generate a RTF file yourself. There is a lovely example of a simple RTF text in Wikipedia:

Saving to RTF example from Wikipedia
{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
This is some {\b bold} text.\par

You will agree that it looks simple, easy to understand and reproduce!

2. For more complicated tasks, there are special RTF converters – for example, the editor developed by our industry colleague – TRichView.

3. Well, and from our point of view using FastReport is the best way! After all, you need to not just make ‘any’ RTF – usually you need it with data, different objects – and here lots of FastReport tools come to help us to create not just formatted text and tables but insert illustrations, barcodes, graphs, indicators, graphic primitives etc.

Creating RTF in Delphi without writing a single line of code!

So, compile the project and implement FastReport with the ‘export to RTF’ component. Generate a report, launch it and click on ‘Save’ in the preview window. Then call export from the preview (below I will describe how to save in RTF format using a Delphi code). The window with export to RTF settings will appear.

RTF images and WYSIWYG setting in Delphi and Lazarus  RichText page settings in Delphi and Lazarus

We can choose which pages of our document to send to Excel, certain pages or a range.

Export settings – whether to insert pictures into RTF, set a better visual correspondence with the original version (WYSIWYG) and use page breaks. You can also repeat the column headers on each page.

RFT is a text page-by-page format and, of course, it supports headers and footers.

You can set up the export of page header and footer in the resulting document (whether to export them as headers and footers, text or exclude them from the file).

Open after export - the resulting file will be opened immediately after export by any software associated with RTF files.

RTF delivery setting

As usual, you can specify where to save your RTF file (in the local storage, send as E-mail, upload to FTP or cloud storage).

And, as promised, here is the code for saving in RTF format directly from Delphi / Lazarus

Saving to RTF
procedure TForm1.Button1Click(Sender: TObject);
 {Generate a report. The report must be generated before exporting}
 {Set the range of pages to export. By default, all pages of the generated report are exported}
 frxRTFExport1.PageNumbers := '2-3';
 {Set whether to export pictures}
 frxRTFExport1.ExportPictures := True;
 {Set whether to export the page breaks so that when printing the pages correspond to the pages of the generated report}
 frxRTFExport1.ExportPageBreaks := True;
 {Set whether to generate a continuous document which skips headers and footers.}
 frxRTFExport1.SuppressPageHeadersFooters := True;
 frxRTFExport1.Wysiwyg := True;
 {Set export mode for page headers and footers
  hfText – as text;
  hfPrint – as headers and footers. SuppressPageHeadersFooters is automatically set to True;
  hfNone - skip}
 frxRTFExport1.HeaderFooterMode := hfText;
 {Set whether to open the resulting file after export}
 frxRTFExport1.OpenAfterExport := False;
 {Set whether to display export progress (show which page is currently being exported)}
 frxRTFExport1.ShowProgress := False;
 {Set whether to display the export filter dialog box}
 frxRTFExport1.ShowDialog := False;
 {Set the name of the resulting file.} 
 {Please note that if you do not set the file name and disable the export filter dialog box,}
 {the file name selection dialog will still be displayed}
 frxRTFExport1.FileName := 'C:\Output\test.rtf';
 {Export the report}

I’d like to note that this method allows you to make a full-fledged RTF document which could initially consist of various elements, including (but not limited to) RTF itself. As a result, you will get a full-fledged RTF document neatly and beautifully formatted according to standards with ability to copy-paste formatted text fragments to other documents. By the way, did you know that RTF has been adopted as an internal standard of technical documentation in some design and engineering companies? We learned this from our customers.