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:
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.
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.
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
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.