Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RTF: Insert Page Break

157 views
Skip to first unread message

Robert F. Tulloch

unread,
Mar 3, 1999, 3:00:00 AM3/3/99
to
Hello:

I am trying to insert page breaks \page in RichEdit to break pages
where I want on TRichedit.Print-Delphi code in ComCtrls doesnt seem to
be structured to detect that when outputting to printer.
Any ideas as to what to do here appreciated.

Thanks.

Best regards

Aron Lopes Petrucci

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to
Robert,

I was working on this thing today. I modified the original code and,
without change the original code. I wrote this procedure,
as a part of a library called "MyPrinter":


//-----------------------------------------------------------------------------

procedure ImprimeRichEditMM(Prt:TPrinter;XLeft,YTop,W,H:Extended;
Red:TRichEdit; FrameRot: Tprocedure); //
printRichEditMM

var
Range: TFormatRange;
LastChar, MaxLen, LogX, LogY, OldMap: Integer;
SaveRect: TRect;


begin
CalculaMedidas;
FillChar(Range, SizeOf(TFormatRange), 0);
Range.hdc := Prt.Handle;
Range.hdcTarget := Range.hdc;
With Range.rc Do
Begin;
Top:= Round(YTop*56.69); // 1mm = 56.69 twips
Left :=Round(XLeft*56.69);
right :=Round((XLeft+W)*56.69);
bottom :=Round((YTop+H)*56.69);
End;
Range.rcPage := Range.rc;
SaveRect := Range.rc;
LastChar := 0;
MaxLen := Red.GetTextLen;
Range.chrg.cpMax := -1;
// ensure printer DC is in text map mode
OldMap := SetMapMode(Range.hdc, MM_TEXT);
SendMessage(Red.Handle, EM_FORMATRANGE, 0, 0); // flush buffer
try
repeat
Range.rc := SaveRect;
Range.chrg.cpMin := LastChar;
LastChar := SendMessage(Red.Handle, EM_FORMATRANGE, 1,
Longint(@Range));
If @FrameRot<>nil then FrameRot;
If (LastChar<MaxLen) and (LastChar<>-1) Then Printer.NewPage;
until (LastChar >= MaxLen) or (LastChar = -1);
finally
SendMessage(Red.Handle, EM_FORMATRANGE, 0, 0); // flush buffer
SetMapMode(Range.hdc, OldMap); // restore previous map mode
end;
End;
//-----------------------------------------------------------------------------

This procedure uses, at least:

var LargPix, // largura da página em pixels
AltPix, // altura da página em pixels
LargMM, // largura da página em mm
AltMM: Integer; // altura da página em mm
ResHor,ResVer: Extended; // resolução horizontal e vertical em
pixel/mm
Orient: TPrinterOrientation; // orientação da página

//-----------------------------------------------------------------------------

procedure CalculaMedidas; // calc measurements
// inicializa as variáveis, seria o "constuctor" se isso fose um objeto
// init the variables. this will be the constructor if I had made an
object like TMyprinter, descendect of TPrinter
begin;
LargMM:=GetDeviceCaps(printer.handle,horzsize); // Largura em mm
AltMM:=GetDeviceCaps(printer.handle,vertsize); // Altura em mm
LargPix:=printer.pagewidth; // Largura em pixels
AltPix:=printer.pageheight; // altura em pixels
ResHor:=LargPix/LargMM; // resolução horizontal [pixels/mm]
ResVer:=AltPix/AltMM; // resolução vertical [pixels/mm]
end;

If you are interested in this complete unit you can E-Mail:
ar...@inbrapenet.com.br

You can use these procedures like this:

Calling ImprimeRichEditMM(Prt:TPrinter;XLeft,YTop,W,H:Extended;
Red:TRichEdit; FrameRot: Tprocedure) will
print the text witch is in "Red", in the area given by position XTop,YTop
with the Width (W) and Then heigth(H), all
in milimeters.

Note the parameter "FrameRot" this is a pointer to a procedure wich will
draw, on the printer.canvas, some type of
frame, page number, or other information. Something like this:

//.............................................................................

procedure FramePg4;

Begin;
Printer.Canvas.Brush.Style:=bsClear; // this is very important
MarcaCantos(Printer.canvas); // this is an other procedure to mark the
page corners.
FormPg4; // a procedure to draw the enterprises logo, borders and other
things
End;
//.............................................................................

in a call like this:

ImprimeRichEditMM(Printer,30,55,163,202,Form1.RE1, @FramePg4);

the contents of RE1 (TRichEdit) will be printed in a 163 x 202 mm area,
with the left top
corner at 30 x 55 mm from the left top corner of the paper. For each page,
FramePg4 procedure
will be called.

If you dont want to call any procedure to draw frames call it like this:

ImprimeRichEditMM(Printer,30,55,163,202,Form1.RE1, nil);

I hope that this would be useful to you

Bye

Aron L. Petruci
ar...@inbrapenet.com.br
Londrina - Pr
Brazil

Robert F. Tulloch

unread,
Mar 6, 1999, 3:00:00 AM3/6/99
to
Aron:

Thanks much. After reading your code I realized where I had goofed up
(not a minor goof-whole philosphy). I then took your lead and solved my
problem: ( only one glitch-I had to set the width of the invisible
RichEdit which is calling this to accomodate full width of text. I guess
if its wrapped in a tiny control, it messes it all up.) I would
appreciate seeing your full code as you offered. Again Thanks.

Best regards

procedure TCreditReport.PrintCreditReport(RTF : TRichEdit);
var
LineIndex, LineMaxIndex : integer;
TempRich : TRichEdit;
Page, Cont, Last : boolean;
begin
if PaginateReport then
begin
TempRich := TRichEdit.CreateParented(CreditReport.Handle);
try
TempRich.Visible := False;
TempRich.Font.Size := CrFont;
TempRich.Font.Name := 'Courier New';
TempRich.Font.Pitch := fpFixed;
TempRich.Clear;

LineIndex := 0;
LineMaxIndex := RTF.Lines.Count-1;
with RTF do
begin
while LineIndex <= LineMaxIndex do
begin

TempRich.Lines.Add(RTF.Lines.Strings[LineIndex]);
Page := (Pos('PAGE',
RTF.Lines.Strings[LineIndex])>1);
Cont := (Pos('CONT',
RTF.Lines.Strings[LineIndex])>1);
Last := (Pos('LAST',
RTF.Lines.Strings[LineIndex])>1);
if (Page) and (Cont) or
((Page) and (Last) and (LineIndex <=
LineMaxIndex)) then
begin
TempRich.Print('');
TempRich.Clear;
end;
LineIndex := LineIndex + 1;
end;
end;
finally
TempRich.Free;
end;
end
else
RTF.Print('');
end;

0 new messages