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

MethodDescription
THPDFTable.Create(ADoc: THotPDF)Creates the table bound to the document that supplies pages, fonts and images
THPDFTable.DestroyFrees the table along with every row and cell it owns
THPDFTable.AddColumn(Width: Single): IntegerAppends a column of the given width in current page units and returns its index
THPDFTable.AddRow: THPDFTableRowAppends 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.RenderMeasures row heights, paints backgrounds, borders and cell content, and carries on onto new pages whilst rows remain

Properties

PropertyDescription
THPDFTable.Doc: THotPDFThe bound document passed to the constructor
THPDFTable.ColCount: IntegerHow many columns were added with THPDFTable.AddColumn
THPDFTable.RowCount: IntegerHow many rows were added with THPDFTable.AddRow
THPDFTable.Rows[Index: Integer]: THPDFTableRowGives access to a row by index
THPDFTable.X: Single and THPDFTable.Y: SingleTable origin on the page in current page units
THPDFTable.HeaderBgColor: TColorBackground colour painted behind header rows
THPDFTable.AlternatingBg: TColorZebra-stripe background applied to alternating non-header rows
THPDFTable.BorderColor: TColor and THPDFTable.BorderWidth: SingleGrid line colour and width used for cell borders
THPDFTable.DefaultFontName: AnsiString, THPDFTable.DefaultFontSize: Single, THPDFTable.DefaultAlign: THPDFCellAlignFallback font and alignment for cells that set none of their own
THPDFTable.Padding: SingleInner spacing between a cell border and its content
THPDFTable.BottomMargin: SingleSpace kept clear at the foot of a page before the render pass breaks to a new page
THPDFTable.RepeatHeader: BooleanWhen true, the header band is emitted again at the top of every continuation page
THPDFTable.TotalHeightRendered: SingleTotal 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

MemberDescription
THPDFTableRow.Create(InitialColCount: Integer)Creates the row pre-filled with the given number of cells
THPDFTableRow.DestroyFrees the row and its cells
THPDFTableRow.AddCell: THPDFTableCellAppends a cell and returns it
THPDFTableRow.EnsureCells(Count: Integer): IntegerGrows the row to at least the given cell count and returns the new count
THPDFTableRow.Cells[Index: Integer]: THPDFTableCellDefault array property giving access to a cell by index
THPDFTableRow.Count: IntegerHow many cells the row currently holds
THPDFTableRow.IsHeader: BooleanMarks the row as a header row painted with THPDFTable.HeaderBgColor and repeated whenever THPDFTable.RepeatHeader is enabled
THPDFTableRow.MinHeight: SingleFloor applied to the measured row height

THPDFTableCell

THPDFTableCell carries the content plus the per-cell overrides layered on top of the table defaults

PropertyDescription
THPDFTableCell.Text: WideStringCell 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: THPDFCellVAlignHorizontal and vertical content alignment inside the cell
THPDFTableCell.ColSpan: Integer and THPDFTableCell.RowSpan: IntegerHorizontal and vertical merge counts, both defaulting to 1
THPDFTableCell.BgColor: TColor and THPDFTableCell.HasBgColor: BooleanPer-cell background colour override and its presence flag
THPDFTableCell.TextColor: TColor and THPDFTableCell.HasTextColor: BooleanPer-cell text colour override and its presence flag
THPDFTableCell.FontName: AnsiString, THPDFTableCell.FontSize: Single, THPDFTableCell.FontStyle: TFontStyles, THPDFTableCell.HasFont: BooleanPer-cell font override and its presence flag
THPDFTableCell.WordWrap: BooleanWraps long text across measured lines inside the cell; on by default
THPDFTableCell.ImageIndex: IntegerWhen zero or higher, the cell shows the image registered under that index through THotPDF.AddImage in place of text; default -1
THPDFTableCell.ImageAspect: SingleDisplay 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: BooleanSends 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

See also