Page 1 of 1

Height of a Row/Paragraph to calculate for printing

Posted: Wed Mar 23, 2011 10:12 am
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

Posted: Wed Mar 23, 2011 1:18 pm
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...)).

Posted: Wed Mar 23, 2011 3:02 pm
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;

Posted: Wed Mar 23, 2011 4:50 pm
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.

Posted: Thu Mar 24, 2011 1:11 pm
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?

Posted: Thu Mar 24, 2011 1:44 pm
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).

Posted: Thu Mar 24, 2011 2:05 pm
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

Posted: Thu Mar 24, 2011 2:10 pm
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.