How to print the content of a RichViewEdit ?

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
armagnac
Posts: 12
Joined: Tue Mar 05, 2024 8:35 am

How to print the content of a RichViewEdit ?

Post 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
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to print the content of a RichViewEdit ?

Post 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)
armagnac
Posts: 12
Joined: Tue Mar 05, 2024 8:35 am

Re: How to print the content of a RichViewEdit ?

Post 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.
armagnac
Posts: 12
Joined: Tue Mar 05, 2024 8:35 am

Re: How to print the content of a RichViewEdit ?

Post 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 !
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to print the content of a RichViewEdit ?

Post 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);
Post Reply