This Topic discusses three possible ways to create and display tabular data in WPTools.
As a fairly simple yet useful example a calculation table should be created. All numbers in the range from 1 to 2 should be multiplied with each other. The square numbers should be bold and the output should be right aligned.
With WPTools just a few lines are required to print such a table and printing can be done by calling the function “Print”.
The first example also features the programmatic activation of paragraph borders and the creation of tabstops.
This is the output:
And here I show the Pascal code which puts each multiplication line from 1 to 20 into a single paragraph:
procedure TForm3.btn20by20Click(Sender: TObject); var i, a : Integer; begin WPRichText1.Header.SetPageWH(0,0,760,360,760,360); WPRichText1.Header.Landscape := true; for i := 1 to 10 do WPRichText1.CurrAttr.AddTab(i*1440, tkRight); for i := 1 to 20 do begin WPRichText1.CurrAttr.SetCellBorders(tsFALSE, tsFalse, tsFalse, tsTRUE, tsTRUE); for a := 1 to 20 do begin if i=a then WPRichText1.CurrAttr.AddStyle([afsBold]); WPRichText1.InputString( Format(#9+'%d * %d = %d', [ i, a, i* a] )); if i=a then WPRichText1.CurrAttr.DeleteStyle([afsBold]); if a=10 then WPRichText1.InputString(#10); end; WPRichText1.InputString(#13); end; end;
But what about tables? They can also be created quite easily with WPTools.
To solve a problem like this I modified the code above to subsequently fill a table.
var i, a : Integer; begin WPRichText1.Header.SetPageWH(0,0,760,360,760,360); WPRichText1.Header.Landscape := true; // Create a table and goto first row WPRichText1.ActiveParagraph := WPRichText1.AddTable(10,40,true); for i := 1 to 20 do begin for a := 1 to 20 do begin if a>10 then WPRichText1.MoveToCell( a-11, (i-1)*2 + 1) else WPRichText1.MoveToCell( a-1, (i-1)*2); WPRichText1.CurrAttr.Alignment := paralRight; WPRichText1.CurrAttr.IndentRight := 36; if i=a then WPRichText1.CurrAttr.AddStyle([afsBold]); WPRichText1.InputString( Format('%d * %d = %d', [ i, a, i* a] )); if i=a then WPRichText1.CurrAttr.DeleteStyle([afsBold]); end; end; end;
With the code above you will probably notice that it takes some time to get executed. The reason for that is that InputString actually simulates keyboard input. The component delays the reformat of the text to the time when all code has been processed, but still, inserting text through InputString is not the fastest.
Fortunately it is easy and straight forward to create real low level code which works with the WPTools paragraphs (also cells are instances of TParagraph) directly.
It is also possible to set styles directly:
Create a style, sid is an integer variable.
with WPRichText1.ParStyles.Add('squarenumbers') do begin sid := id; TextStyle.ASetCharStyle( tstrue, WPSTY_Bold ); TextStyle.ASetColor(WPAT_BGColor, $E0E0E0); end;
The style ID can be assigned to the Style property of any paragraph or cell.
The code below works with low level table creation. It is executed basically instantly.
It does not matter if the I loop goes up to 20, 200, or even 1000.
In the last case it would create over 90 pages and display them the moment the button was pressed!
procedure TForm3.CreateTableFastClick(Sender: TObject); var aTable, aRow, aCell : TParagraph; i, a, sid : Integer; begin WPRichText1.Header.SetPageWH(0,0,760,360,760,360); WPRichText1.Header.Landscape := true; // We create a style to highlight the square numbers, such as 2*2=4 with WPRichText1.ParStyles.Add('squarenumbers') do begin sid := id; TextStyle.ASetCharStyle( tstrue, WPSTY_Bold ); TextStyle.ASetColor(WPAT_BGColor, $E0E0E0); end; // Low level table creation // First create the parent table object aTable := WPRichText1.ActiveText.AppendTable(); for i := 1 to 1000 do begin // In the parent table append a new row aRow := aTable.AppendNewRow(); for a := 1 to 20 do begin // In the row create a cell aCell := aRow.AppendNewCell(); // Set the border aCell.ASetBorderFlags(15); // the alignment aCell.ASet(WPAT_Alignment, Integer(paralRight)); // and for squere numbers use the style created above if i=a then aCell.Style := sid; aCell.SetText( Format('%d * %d = %d', [ i, a, i*a] ) ); if a=10 then aRow := aTable.AppendNewRow(); end; end; // Dont forget to call ReformatAll since WPTools does not // call it automatically after low-level changes to the text WPRichText1.ReformatAll(); end;
The code above also uses a special property: WPRichText1.ActiveText. This is the layer which currently has the cursor focus. It can be the body text, a header or footer text, or, with the Premium edition, also a footnote or textbox. It is an instance of the class “TWPRTFDataBlock“.
Der Beitrag WPTools: How to quickly create and print a multiplication table erschien zuerst auf WPCubed.