YOUSEFUL / DELPHI / COMPONENTS
TAtlasPrintDocument
Paginates the current view of a TAtlasTable into fixed-size pages for preview, custom drawing or the Windows printer.
Description
TAtlasPrintDocument turns the rows a TAtlasTable currently shows (after its filter and sort) into a numbered sequence of pages. Prepare takes a snapshot of the table and the page geometry; from then on PageCount, FirstRow, RowsOnPage, RenderPage, Preview and Print work on that snapshot, and later changes to the table are not reflected until you call Prepare again.
Every page shows the bold Title at the top, the column headers in a shaded row, up to RowsOnPage data rows with single-line, ellipsized text (line breaks in cell values become spaces), and Page n of m at the bottom. Column widths are taken from TAtlasColumn.Width and scaled in proportion to fill the printable width. The page geometry is expressed in abstract units; the defaults, 794 × 1123 with a 48 unit margin and 28 unit rows, are A4 at 96 dpi, so a Preview bitmap is the page at screen resolution while Print scales the same layout to the printer page.
This is a table report, not a flowing report designer: no grouping, images, page headers beyond the title, or rich text. Print uses Vcl.Printers and the default Windows printer; the product's validation has generated and inspected previews but has not exercised actual printer output. The component is meant for the VCL UI thread. Errors raise EAtlasPrintError.
Members
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Creates an empty document with Title set to Atlas report and no pages (PageCount is 0 until Prepare succeeds). |
Prepare | method | Snapshots the visible rows of Table (its current filtered and sorted view, via ExportCSV(True)) together with the column widths, and fixes the page geometry. |
FirstRow | method | Zero-based index, into the snapshot's data rows, of the first row printed on the zero-based Page. |
RowsOnPage | method | Number of data rows printed on Page: the rows per page on every page except the last, which carries the remainder. |
RenderPage | method | Draws Page onto Canvas, scaling the page geometry to fit Bounds exactly (both axes are scaled independently, so keep the aspect ratio of Bounds equal to the page's to avoid distortion). |
Preview | method | Returns a new TBitmap of PageWidth × PageHeight pixels (794 × 1123 by default) with Page rendered on it. |
Print | method | Sends every page to the default Windows printer through Vcl.Printers: sets Printer.Title to Title, calls BeginDoc, renders each page scaled to the printer's page size with NewPage in between, then EndDoc. |
PageCount | property | Number of pages in the snapshot: 0 before a successful Prepare, otherwise at least 1 (a table without rows gives a header-only page). |
Title | property | Text drawn in bold at the top of every page and used as the print job name. |
Constructors
Create constructor
constructor Create(AOwner: TComponent); override;Creates an empty document with Title set to Atlas report and no pages (PageCount is 0 until Prepare succeeds).
Properties
PageCount property
property PageCount: Integer read GetPageCount;Number of pages in the snapshot: 0 before a successful Prepare, otherwise at least 1 (a table without rows gives a header-only page).
Title property
property Title: string read FTitle write FTitle;Text drawn in bold at the top of every page and used as the print job name. Defaults to Atlas report. Changing it after Prepare affects subsequent rendering; it is not part of the snapshot.
Methods
Prepare method
procedure Prepare(Table: TAtlasTable; PageWidth: Integer = 794; PageHeight: Integer = 1123; Margin: Integer = 48; RowHeight: Integer = 28);Snapshots the visible rows of Table (its current filtered and sorted view, via ExportCSV(True)) together with the column widths, and fixes the page geometry. Rows per page is (PageHeight - 2 * Margin - 3 * RowHeight) div RowHeight: the space left after the margins, the title line, the header row and the footer line. Raises EAtlasPrintError when Table is nil or has no columns, when PageWidth or PageHeight is below 100, Margin is negative or more than a third of either dimension, RowHeight is below 8, or no data row fits. On failure the previous snapshot is kept. A table without rows still produces one page (header only).
Document.Title := 'Invoices / September';
Document.Prepare(Table); // A4 portrait at 96 dpi, 33 rows per page
Document.Prepare(Table, 1123, 794); // landscape: 21 rows per pageFirstRow method
function FirstRow(Page: Integer): Integer;Zero-based index, into the snapshot's data rows, of the first row printed on the zero-based Page. It equals Page * rows per page. Raises EAtlasPrintError when Page is outside 0..PageCount - 1, including on an unprepared document.
RowsOnPage method
function RowsOnPage(Page: Integer): Integer;Number of data rows printed on Page: the rows per page on every page except the last, which carries the remainder. Summing RowsOnPage over all pages gives the number of snapshot rows. Raises EAtlasPrintError for a page out of range.
Total := 0;
for I := 0 to Document.PageCount - 1 do
Inc(Total, Document.RowsOnPage(I)); // Total = number of rows in the snapshotRenderPage method
procedure RenderPage(Canvas: TCanvas; const Bounds: TRect; Page: Integer);Draws Page onto Canvas, scaling the page geometry to fit Bounds exactly (both axes are scaled independently, so keep the aspect ratio of Bounds equal to the page's to avoid distortion). Fills the background white, draws the title in bold Segoe UI, the header row on a light shade, cell borders, ellipsized cell text and the page footer. Raises EAtlasPrintError when Page is out of range or Bounds has no positive width and height. Use it for thumbnails, custom preview controls or your own printing loop.
Thumb := TBitmap.Create;
try
Thumb.SetSize(397, 562); // half of an A4 page at 96 dpi
Document.RenderPage(Thumb.Canvas, Rect(0, 0, 397, 562), 0);
finally
Thumb.Free;
end;Preview method
function Preview(Page: Integer): TBitmap;Returns a new TBitmap of PageWidth × PageHeight pixels (794 × 1123 by default) with Page rendered on it. The caller owns and frees the bitmap. Raises EAtlasPrintError for a page out of range.
Page := Document.Preview(0);
try
Image.Picture.Bitmap.Assign(Page);
finally
Page.Free;
end;Print method
procedure Print;Sends every page to the default Windows printer through Vcl.Printers: sets Printer.Title to Title, calls BeginDoc, renders each page scaled to the printer's page size with NewPage in between, then EndDoc. If rendering raises, the print job is aborted and the exception propagates. Raises EAtlasPrintError when no document has been prepared. Requires an installed printer; actual printer output is listed as pending manual acceptance in the product's validation record.
Examples
Preview the first page as a PNG file
Prepares a report from a table and saves page 1 at A4 size; the caller owns the preview bitmap.
uses System.SysUtils, Vcl.Graphics, Vcl.Imaging.pngimage, Atlas.Data, Atlas.Printing;
procedure SaveFirstPage(Table: TAtlasTable; const FileName: string);
var
Document: TAtlasPrintDocument;
Page: TBitmap;
Png: TPngImage;
begin
Document := TAtlasPrintDocument.Create(nil);
try
Document.Title := 'Invoices / September';
Document.Prepare(Table); // snapshot of the current view, A4 at 96 dpi
Page := Document.Preview(0); // caller owns the bitmap
try
Png := TPngImage.Create;
try
Png.Assign(Page);
Png.SaveToFile(FileName);
finally
Png.Free;
end;
finally
Page.Free;
end;
Writeln(Format('%d pages, %d rows on the first', [Document.PageCount, Document.RowsOnPage(0)]));
finally
Document.Free;
end;
end;Related
EAtlasPrintError · TAtlasTable · TAtlasPivot · TAtlasGrid
Also in Atlas VCL
TAtlasTable · TAtlasGrid · TAtlasValueEdit · TAtlasTree · TAtlasWorkbook · TAtlasSheetGrid · TAtlasPivot · TAtlasInspector · TAtlasRichEdit · TAtlasSpellChecker · TAtlasChart · TAtlasGauge · TAtlasThemer · TAtlasWizard · TAtlasWorkspace · TAtlasCommandBar · TAtlasActionTile · TAtlasTileBoard · TAtlasNavigation · TAtlasMap · TAtlasGraph · TAtlasDiagram · TAtlasSchedule · TAtlasScheduler · TAtlasTaskPlan · TAtlasGantt · TAtlasPdfDocument · TAtlasPdfViewer