Printing RichText across multiple pages

Category

Code Based - General

Question

How do I print RichText across multiple pages?

Solution

It is often necessary to print memos containing RTF across multiple pages. This can easily be done with the following code:

Delphi Example:

uses
  RPMemo;

procedure TForm1.ReportSystem1Print(Sender: TObject);
var  MemoBuf: TMemoBuf;
begin
  With Sender as TBaseReport do begin
    MemoBuf := TMemoBuf.Create;
    MemoBuf.BaseReport := Sender as TBaseReport;
    try
      MemoBuf.RichEdit := RichEdit1;
      MemoBuf.PrintStart := 1.0;
      MemoBuf.PrintEnd := 7.0;
      While not MemoBuf.Empty do begin
        MemoBuf.PrintHeight(SectionBottom - LineTop,false);
        If not MemoBuf.Empty then begin
          NewPage;
        end; { if }
      end; { while }
    finally
      MemoBuf.Free;
    end; { tryf }
  end; { with }
end;

C++Builder Example:

#include "RPMemo.hpp"

void __fastcall TForm1::ReportSystem1Print(TObject *Sender)
{
  TMemoBuf* MemoBuf;
  TBaseReport* bp = dynamic_cast(Sender);

  MemoBuf = new TMemoBuf();
  MemoBuf->BaseReport = bp;
  try {
    MemoBuf->RichEdit = RichEdit1;
    MemoBuf->PrintStart = 1.0;
    MemoBuf->PrintEnd = 7.0;
    while (!MemoBuf->Empty()) {
      MemoBuf->PrintHeight(bp->SectionBottom - bp->LineTop,false);
      if (!MemoBuf->Empty()) {
        bp->NewPage();
      }//if
    }//while
  }// try
  __finally {
    delete MemoBuf;
  }// tryf
}

PrintHeight is a function that is new to RP3. It was added to provide a better solution than using the PrintMemo method that was available in RP2. PrintMemo is used to print standard memos and does this very well. However, PrintMemo is not very good for printing RTF since RTF most often contains lines of different heights and PrintMemo assumes all lines are the same height. This can lead to extra space at the bottom of the page or lines that are printed beyond the desired location. PrintHeight will look ahead and figure out how many lines of RTF will fit on a page, taking into consideration the varying heights, and will print the information correctly. PrintHeight is a function that will return the height that it actually printed (if you need to know that, but normally you won't).