Example: Print a page number

Top  Previous  Next

In this example we insert a text object and create a class item to print a page number:


1) Insert the text object


procedure TForm1.bMyPageNrClick(Sender: TObject);





We create a text object with the name 'MYPAGE'. The Source property is '' and the Params property, which will be printed by default will be '---'.


Without any event handling we will see --- inplace of this object.


2) Create a TWPTextObjectClasses object




3) Connect the TWPRichText to this component using the property WPTextObjectClasses



4) Add a new item to the classes collection




The item should be used for all TWPTextObj with the name "MYPAGE".


5) We add an event handler for OnObjectCassGetText




procedure TForm1.WPTextObjectClasses1Items2ObjectClassGetText(

 Sender: TWPCustomRTFControl; Memo: TWPRTFEngineBasis;

 CallMode: TWPMeasureObjectMode; ObjClass: TWPTextObjectClass;

 TextObj: TWPTextObj; var Text: WideString; PaintingPage: TWPVirtPage;

var Handled: Boolean);


if PaintingPage=nil then

      Text := IntToStr( TWPCustomRTFedit(Sender).PageCount) // Reserve space

else Text := IntToStr(PaintingPage.PageNr);       // Paint




This event handler needs to work also when PaintingPage = nil - in this case it was called during reformat and initialization of the text.




Note: Fortunately it is not required to always implement this code just to print a page number. The text objects with the name "PAGE" will do it automatically.