B4J Library RTF print module for formatted reports

RTFPrint module, print formatted reports for B4J applications

What it does:

The Module RTFPrint consists of subroutines that support the printing of formatted reports.

It has been written because up to now there is no library in B4J that allows full control of contents and/or formats.

RTFPrint generates a rich text formatted file which can then be opened or directly printed with the Windows intrinsic Wordpad. Why RTF? Because it allows the required functions performed without the use of extra licensed software like MS Excel, MS Access, PDF and CSV converters.

RTFPrint should also work on Linux or Mac computers provided that a a substitute for Wordpad is found and chosen.

With the proper adaptions it can be used for any type of documents like automated letters etc. There is no limit to alterations like group calculations, multiple table printouts etc.

What it does not

It does not provide a GUI. You have to change the code to change the appearance. The code carries some explaining comments.

RTFPrint does not work with the DOS Print.exe as this program ignores RTF commands.

The application must control some parameters like max. lines per page and the inclusion of footers and headers programmatically as Wordpad does not understand commands like \RTF header or footer and does not handle automatic page roll over.


Formatting reports is a delicate matter. Although you can change appearances like font and fontsize, this program is configured to provide the maximum row length on A4 landscape paper size with an 8pt courier font for the table details. This is the smallest font commonly readable and has a fixed pitch font which is mandatory for table printouts. If you change this, you will have to adapt quite a few parameters like calculation of tabs, lines per page etc.

The field width of table details is defined in a list together with the header text. Contents passed into the fields (in the sub Detailline) is truncated to the max number of characters defined in this list as otherwise the printout busts tabulators and becomes unreadable. If you change font size you must adapt this calculation.


Wordpad does not understand most of the more elaborate rtf commands like page orientation and page margins, footers and so on. Pageorientation and margins can and must be setup in Wordpad which stores these parameters in the registry so this must be done only once. However, on a specific installation, these params are fixed. If this is not good enough, then Wordpad must be substituted by other programs that fully understand rtf (like MS word or open office Writer etc.).

RTF uses ANSI coding for text while B4J uses UTF-8. While the basic English alphabet is compatible, special characters used in other languages are not. The author lives in Switzerland where german Umlauts like äöü are used and french characters like é and à too. There is a case statement in RTF print that allows the substitution of such characters in an rtf compatible way, which is \u and the decimal value of the UTF-8 code. I think changing this is more or less self-explaining.

If the formatted text for a landscape printout is passed directly via Wordpad to the printer, the printer's standard page orientation must be defined as „landscape“. It is easy to clone a printer instance and name it accordingly and there are instructions to do so on the internet. Basically add a printer with the driver name of the original and configure it accordingly.

The test programm writes a file to the root directory C: . You may want to delete this if not used anymore. Of course, the path can be changed at your descretion. In my Windows 7 installation, the internal paths of B4J are write protected for unknown reasons.

The zip file

The zip file contains a testprogramm RTFPrintTest which includes the module RTFprint. Copy this module to your own application and use it as demonstrated in the testprogram.

Good luck! Georg


  • RTFPrintTest.zip
    23.5 KB · Views: 517
Last edited: