How to create a file in TXT format from Delphi / C++Builder / Lazarus

The TXT file stores text documents with information organized in the form of lines. In modern systems, strings are separated by line separators; in the past, storing strings in the form of records of constant or variable length was used. Sometimes the end of a text file (especially if the file size information is not stored in the file system) is also marked with one or more special characters known as end-of-file markers. It is important to note that a text file can contain both formatted and unformatted text (sometimes the letter spacing and font boldness / italicization properties are regulated by special esc-sequences – control characters).

Today we are used to the fact that font formatting, the elements of the style, certain sizes according to user preferences can be applied for any symbol. There are more specialized formats like XML, PHP, CSV and CHM; the TXT format is laid in their structure as a basis.

You can open a TXT file not only within a computer, but also on a variety of mobile phones, smartphones, tablets and special readers, so you can work with Text File on different versions of Microsoft Windows, Mac OS, Linux, iOS, Android and Windows Mobile platforms.
Most users of the Microsoft system use a regular Notepad or the popular Microsoft Office software package in order to open TXT and users of the Mac OS use the built-in TextEditor.

Why might we need just the plain text? For example, powerful high-performance printing devices in banks still print plain text (yes, formatted – but text). And from the point of view of information storage compactness without additional compression, there is nothing like it. Corporate sector loves “plain TXT”.

Creating TEXT in Delphi without a single line of code!

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

TXT setting  TXT setting

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

Export settings – whether to export the page breaks, create frames, add empty lines (after each line of the report), use OEM codepage (Windows encoding is set as default).

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

Open after export – the resulting file will be opened immediately after export by any software associated with TXT documents (Notepad, TextEditor).

Code for saving in TXT format directly from Delphi / Lazarus

Saving in TXT
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}
 frxSimpleTextExport1.PageNumbers := '2-3';
 {Set whether to export the page breaks}
 frxSimpleTextExport1.PageBreaks := True;
 {Set whether to export the frames of the generated report objects using pseudo-graphics characters}
 frxSimpleTextExport1.Frames := False;
 {Set whether to export empty lines}
 frxSimpleTextExport1.EmptyLines := False;
 {Set whether to export the text in OEM or Windows encoding}
 frxSimpleTextExport1.OEMCodepage := False;
 {Set whether to open the resulting file after export}
 frxSimpleTextExport1.OpenAfterExport := False;
 {Set whether to display export progress
  (show which page is currently being exported)}
 frxSimpleTextExport1.ShowProgress := False;
 {Set whether to display the export filter settings dialog box}
 frxSimpleTextExport1.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}
 frxSimpleTextExport1.FileName := 'C:\Output\test.txt';
 {Export the report}

Now we can save complex documents in a regular TXT format file! By the way, this is not the only way to create complex but entirely text documents. Please note that when you select File – New… (with ellipsis) in the FastReport report designer a window for choosing what exactly you want to create will appear. 

New Item

Select “Dot-Matrix Report” and go into the TXT-report creation mode. But we will talk more about this another time. 

Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • 901 N Pitt Str #325 Alexandria VA 22314

© 1998-2024 Fast Reports Inc.