THPDFTable Table Layout and Rendering
THPDFTable is the lightweight table layout and rendering engine that arrived in v2.429.0 and is exposed by the HPDFTable unit
Before v2.429.0, producers had to draw cells by hand with Rectangle plus TextOut and hard-coded column X coordinates, handling row heights, zebra striping and page breaks themselves; THPDFTable drives the existing THPDFPage drawing primitives, so tables render with measured row heights, merged cells and automatic page-break continuation
Unit
HPDFTable
Example of use
uses
HPDFTable;
var
Tbl: THPDFTable;
Row: THPDFTableRow;
begin
Tbl:= THPDFTable.Create(HPDF);
try
Tbl.AddColumn(80); Tbl.AddColumn(200); Tbl.AddColumn(120);
Tbl.SetBounds(50, 60);
Row:= Tbl.AddRow; Row.IsHeader:= True;
Row.Cells[0].Text:= 'No';
Row.Cells[1].Text:= 'Description';
Row:= Tbl.AddRow;
Row.Cells[0].ColSpan:= 2; // merged cell across columns 0..1
Row.Cells[0].Text:= 'merged header';
Tbl.Render;
finally
Tbl.Free;
end;
end;
THPDFTable
THPDFTable owns the column layout, the row collection and the render pass; it draws through the THotPDF component handed to THPDFTable.Create and touches only public THPDFPage and THotPDF members
Methods
| Method | Description |
|---|---|
THPDFTable.Create(ADoc: THotPDF) | Creates the table bound to the document that supplies pages, fonts and images |
THPDFTable.Destroy | Frees the table along with every row and cell it owns |
THPDFTable.AddColumn(Width: Single): Integer | Appends a column of the given width in current page units and returns its index |
THPDFTable.AddRow: THPDFTableRow | Appends a row sized to the current column count and hands it back |
THPDFTable.SetBounds(AX, AY: Single) | Positions the table origin on the current page |
THPDFTable.Render | Measures row heights, paints backgrounds, borders and cell content, and carries on onto new pages whilst rows remain |
Properties
| Property | Description |
|---|---|
THPDFTable.Doc: THotPDF | The bound document passed to the constructor |
THPDFTable.ColCount: Integer | How many columns were added with THPDFTable.AddColumn |
THPDFTable.RowCount: Integer | How many rows were added with THPDFTable.AddRow |
THPDFTable.Rows[Index: Integer]: THPDFTableRow | Gives access to a row by index |
THPDFTable.X: Single and THPDFTable.Y: Single | Table origin on the page in current page units |
THPDFTable.HeaderBgColor: TColor | Background colour painted behind header rows |
THPDFTable.AlternatingBg: TColor | Zebra-stripe background applied to alternating non-header rows |
THPDFTable.BorderColor: TColor and THPDFTable.BorderWidth: Single | Grid line colour and width used for cell borders |
THPDFTable.DefaultFontName: AnsiString, THPDFTable.DefaultFontSize: Single, THPDFTable.DefaultAlign: THPDFCellAlign | Fallback font and alignment for cells that set none of their own |
THPDFTable.Padding: Single | Inner spacing between a cell border and its content |
THPDFTable.BottomMargin: Single | Space kept clear at the foot of a page before the render pass breaks to a new page |
THPDFTable.RepeatHeader: Boolean | When true, the header band is emitted again at the top of every continuation page |
THPDFTable.TotalHeightRendered: Single | Total height the last render pass consumed across all pages |
THPDFTableRow
THPDFTableRow holds one table row's cells together with the row-level flags used during measurement and rendering
| Member | Description |
|---|---|
THPDFTableRow.Create(InitialColCount: Integer) | Creates the row pre-filled with the given number of cells |
THPDFTableRow.Destroy | Frees the row and its cells |
THPDFTableRow.AddCell: THPDFTableCell | Appends a cell and returns it |
THPDFTableRow.EnsureCells(Count: Integer): Integer | Grows the row to at least the given cell count and returns the new count |
THPDFTableRow.Cells[Index: Integer]: THPDFTableCell | Default array property giving access to a cell by index |
THPDFTableRow.Count: Integer | How many cells the row currently holds |
THPDFTableRow.IsHeader: Boolean | Marks the row as a header row painted with THPDFTable.HeaderBgColor and repeated whenever THPDFTable.RepeatHeader is enabled |
THPDFTableRow.MinHeight: Single | Floor applied to the measured row height |
THPDFTableCell
THPDFTableCell carries the content plus the per-cell overrides layered on top of the table defaults
| Property | Description |
|---|---|
THPDFTableCell.Text: WideString | Cell text; rendered through THPDFPage.TextOutHTML when THPDFTableCell.IsHTML is true, so inline markup for bold, colour, links and sub-sup is honoured |
THPDFTableCell.Align: THPDFCellAlign and THPDFTableCell.VAlign: THPDFCellVAlign | Horizontal and vertical content alignment inside the cell |
THPDFTableCell.ColSpan: Integer and THPDFTableCell.RowSpan: Integer | Horizontal and vertical merge counts, both defaulting to 1 |
THPDFTableCell.BgColor: TColor and THPDFTableCell.HasBgColor: Boolean | Per-cell background colour override and its presence flag |
THPDFTableCell.TextColor: TColor and THPDFTableCell.HasTextColor: Boolean | Per-cell text colour override and its presence flag |
THPDFTableCell.FontName: AnsiString, THPDFTableCell.FontSize: Single, THPDFTableCell.FontStyle: TFontStyles, THPDFTableCell.HasFont: Boolean | Per-cell font override and its presence flag |
THPDFTableCell.WordWrap: Boolean | Wraps long text across measured lines inside the cell; on by default |
THPDFTableCell.ImageIndex: Integer | When zero or higher, the cell shows the image registered under that index through THotPDF.AddImage in place of text; default -1 |
THPDFTableCell.ImageAspect: Single | Display aspect ratio for an image cell; the image is scaled to the cell width and its height follows from this ratio, default 1.0 |
THPDFTableCell.IsHTML: Boolean | Sends THPDFTableCell.Text through THPDFPage.TextOutHTML so inline markup gets rendered |
Alignment enums
THPDFCellAlign = (caLeft, caCenter, caRight) picks the horizontal cell alignment and THPDFCellVAlign = (cvaTop, cvaMiddle, cvaBottom) the vertical one
Layout and page-break behaviour
Row heights are measured from cell content with word wrap and THPDFTableRow.MinHeight honoured; spanning cells sit on an internal grid so THPDFTableCell.ColSpan and THPDFTableCell.RowSpan merge correctly across rows; and the render pass breaks to a new page when the next row will not fit above THPDFTable.BottomMargin
With THPDFTable.RepeatHeader true, the header band is emitted again at the top of every continuation page before any further rows are drawn
Image cells, introduced in v2.432.0, reference by index an image registered through THotPDF.AddImage, scale to the cell width and take their height from THPDFTableCell.ImageAspect, so the picture keeps its source aspect ratio