Height of a Row/Paragraph to calculate for printing

General TRichView support forum. Please post your questions here
Post Reply
ThYpHoOn
Posts: 21
Joined: Fri Jul 16, 2010 1:56 pm
Location: Germany

Height of a Row/Paragraph to calculate for printing

Post by ThYpHoOn »

Hi folks,

i'm experimenting some methods to calculate the rtf text height for printing and cutting the rtf for a pagebreak to the next page.

Example: I'm having a rectangle of 150px * 50px on page 1 and a random rtf text which should be printed in the rectangle, but if the height is higher than 50px it should print all that fit in the rectangle and giving a carret position to print the rest on page 2.

Possible calculation: I'm calculating the height of every paragraph and compare if it fit in the rectangle. If its not fit, then i know there must be a pagebreak.

I know there must be a function for that but i haven't found it directly.

Tahnks for help,
greetings ThYpHoOn
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Paragraph size is known only when it is already formatted for printing (if you use RVPrint, when RVPrint.FormatPages is called).

Sorry, I do not understand what you want to implement. Very probably, there is a simple solution for you problem, without paragraph size calculation.
For example, if you need printing in 150x50px, you can define this size in RVReportHelper (Init(...150...), FormatNextPage(...50...)).
ThYpHoOn
Posts: 21
Joined: Fri Jul 16, 2010 1:56 pm
Location: Germany

Post by ThYpHoOn »

Thanks Sergey, this looks good.

Where could i get the carret-position if it not fit into one page?

Here is an snip of my code:

Code: Select all

function CalcRTFText (const cRTF:String; const nHeight:Integer; const nWidth:Integer; var nPos:Integer):String;
var rvh     : TRVReportHelper;
    oStream : TStringStream;
begin
     rvh := TRVReportHelper.Create(NIL);

     oStream := TStringStream.Create(cRTF);
     rvh.Richview.LoadRTFFromStream(oStream);
     rvh.Init(Printer.Canvas,nWidth);

     if not(rvh.FormatNextPage(nHeight)) then begin
     //if (rvh.PagesCount = 1) then
        nPos   := 0;
        Result := cRTF; //the full input rtf is the result, because all fit
     end else begin
        // here i need the carret-position (nPos) 
     end;

     //Freeing Memory
     FreeAndNil(oStream);
     FreeAndNil(rvh);

end;
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

RVReportHelper.GetFirstItemOnPage returns the position at the beginning of the page. However, the page must be formatted, so I suggest to call FormatNextPage until the whole document is formatted, then use GetFirstItemOnPage.

And, if you use tables, there is no such thing as caret position at the beginning of page. Because page break may cross several cells.
ThYpHoOn
Posts: 21
Joined: Fri Jul 16, 2010 1:56 pm
Location: Germany

Post by ThYpHoOn »

Which function should i use for the RTF-Text that didn't fit on the first page?
I've got the ItemNo and offset with

Code: Select all

rvh.GetFirstItemOnPage(2,nItemNo,nOffset);
But how could i use this information to get the rtf-text at this point?
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do you want to get text after this point?
Then select and save selection:
rv.SetSelectionBounds(nItemNo, nOffset, rv.ItemCount-1, rv.GetOffsAfterItem(rv.ItemCount-1));
rv.SaveRTFToStream(Stream, True);
Unfortunately, you cannot select in TRVReportHelper, so you need a copy of this text in TRichView.

Besides, TRVReportHelper can save text in a page in RVF format: rvh.SavePageAsRVF(Stream, PageNo).
ThYpHoOn
Posts: 21
Joined: Fri Jul 16, 2010 1:56 pm
Location: Germany

Post by ThYpHoOn »

Sergey Tkachenko wrote: Besides, TRVReportHelper can save text in a page in RVF format: rvh.SavePageAsRVF(Stream, PageNo).
Yeah thats exactly what i want, is there any important difference between RVF/RTF or could i load the RVF simple to a TRichView and load there the rtf text?


Greetz, ThY
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Yes, to convert RVF to RTF, load RVF in a hidden TRichView and save as RTF.

By the way, SavePageAsRVF supports page breaks in tables, so it's better than using GetFirstItemOnPage.
Post Reply