Page 1 of 1

How to print the content of a RichViewEdit ?

Posted: Fri Mar 08, 2024 9:42 am
by armagnac
Hi,

With a TRichEdit, I use this code :

///////////////////////////////////////////////////////////////////
RE is the RichEdit

var Range : TFormatRange;
SaveRect: TRect;
TextLenEx: TGetTextLengthEx;
LastChar, MaxLen : integer;

/////////////////////////////// printing code
FillChar(Range, SizeOf(TFormatRange), 0);
with Range
do begin
hdc := tiPrinter.Canvas.Handle;
hdcTarget := tiPrinter.Canvas.Handle;
// a particular area on the sheet defined here /// MultDiv(a, b, c) = (a * b) div c
RC.Top := MultDiv(yTop, TwipsPerInch, tiPrintCaps.YPixelsPerInch);
RC.Left := MultDiv(xLeft, TwipsPerInch, tiPrintCaps.XPixelsPerInch);
RC.Bottom := MultDiv(yBottom, TwipsPerInch, tiPrintCaps.YPixelsPerInch);
RC.Right := MultDiv(xRight, TwipsPerInch, tiPrintCaps.XPixelsPerInch);

rcPage := RC;
SaveRect := RC;
end;

LastChar := 0;

with TextLenEx
do begin
Flags := GTL_DEFAULT;
codepage := CP_ACP;
end;

MaxLen := RE.Perform(EM_GETTEXTLENGTHEX, WParam(@TextLenEx), 0);
Range.chrg.cpMax := -1;

repeat
Range.RC := SaveRect;
Range.chrg.cpMin := LastChar;
LastChar := SendMessage(RE.Handle, EM_FORMATRANGE, 1, Longint(@Range));
{
if (LastChar < MaxLen) and (LastChar <> -1) then
NewPage; }
until (LastChar >= MaxLen) or (LastChar = -1);

SendMessage(RE.Handle, EM_FORMATRANGE, 0, 0); { flush buffer }
/////////////////////////////////////////////////////////////////////////

This code works fine. But how to proceed with TRichViewEdit ? Because if I only change from RE to RVE, nothing is printed.

Thank you

Re: How to print the content of a RichViewEdit ?

Posted: Fri Mar 08, 2024 12:14 pm
by Sergey Tkachenko
1. Use the global Printer object from Printers unit to choose the current printer and define page format.
(See the last code sample here how to set page size).

2. Place TRVPrint component on form. Define margins and additional page settings in properties of this component.

3. Code for printing:

Code: Select all

RVPrint1.AssignSource(RichViewEdit1);
RVPrint1.FormatPages(rvdoAll);
RVPrint1.Print('My document', 1, False);
There are several demo projects that implement printing. For example "TRichView\Demos\DelphiUnicode\Editors\Editor 1\"



If you use RichViewActions, you can simply use TrvActionPrint or TrvActionQuickPrint. To use, you need TRVPrint component assigned to RVPrint property of RVAControlPanel. Assign the action to a button or a menu item.
No code is needed.
There are also actions for page setup and print preview.
See "RichViewActions\Demos\DelphiUnicode\ActionTest_MultiRes\" demo (for Delphi 10.3 or newer; there are similar demos for older versions of Delphi)

Re: How to print the content of a RichViewEdit ?

Posted: Fri Mar 08, 2024 12:54 pm
by armagnac
A Printer is already in use before I want to print the content of the TrichViewEdit, because there are lot of informations printed on a A4 page, and the content of the TRichViewEdit is just a part of them.

Re: How to print the content of a RichViewEdit ?

Posted: Fri Mar 08, 2024 1:26 pm
by armagnac
I have found a solution, not clever but it works !

rve.SaveRTF('board.rvf', false);
RE.Lines.LoadFromFile('board.rvf');
RE.PlainText := false;

where RVE is the TRichViewEdit and RE a TJvRichEdit.

So I can use the previous code !

Re: How to print the content of a RichViewEdit ?

Posted: Fri Mar 08, 2024 5:13 pm
by Sergey Tkachenko
If you transfer document from TRichViewEdit to TJvRichEdit, you lose all formatting and objects unsupported by TJvRichEdit.

As I said, you need only 3 lines of code to print TRichViewEdit:

Code: Select all

RVPrint1.AssignSource(RichViewEdit1);
RVPrint1.FormatPages(rvdoAll);
RVPrint1.Print('My document', 1, False);