TGridPrinter is a component to simplify printing of string grids or other descendants of TCustomGrid. It is bundled together with a ready-made print preview dialog, TGridPrintPreviewDialog, as well as standard actions to trigger printing or to show the preview dialog without writing a single line of code.
Author: Werner Pamler
License: Modified LGPL-2 (with linking exception, like Lazarus LCL).
The package is made available for installation by the Online-Package-Manager (OPM). Additionally, a zip file with the most recent release version can be found at Lazarus CCR at SourceForge. Unzip the file into any folder, load gridprinterpkg.lpk into Lazarus and click Use > Install.
The current development version is hosted at Lazarus CCR (SVN). Use SVN to check it out, or download the zipped snapshot from that page. Install by loading gridprinterpkg.lpk and clicking Use > Install.
- Drop a
TGridPrintercomponent on the form. - Link its
Gridproperty to the StringGrid you want to print. - In the
OnClickhandler of a button or menu item, call thePrintmethod.
procedure TForm1.PrintButtonClick(Sender: TObject);
begin
GridPrinter1.Print;
end;- Drop a
TGridPrintPreviewDialogon the form. - Link its
GridPrinterproperty to theTGridPrinterinstance. - Call the
Executemethod of the preview dialog.
procedure TForm1.PrintPreviewButtonClick(Sender: TObject);
begin
GridPrintPreviewDialog1.Execute;
end;The preview dialog allows you to scroll pages, zoom, change orientation, adjust margins by dragging, and add headers/footers – and print from there as well.
TGridPrinter has a property ShowPrintDialog which controls whether a print dialog is shown before printing. The possible values are:
gpdNone– no dialoggpdPageSetup– showsTPageSetupDialog(paper size, orientation, margins)gpdPrintDialog– showsTPrintDialog(printer, copies, page range)gpdPrinterSetup– showsTPrinterSetupDialog(printer, paper size, orientation)
Both the grid and the TGridPrinter fire an OnPrepareCanvas event. You can share the same event handler, but you must use the correct canvas depending on the sender.
procedure TForm1.PrepareCanvasHandler(Sender: TObject; ACol, ARow: Integer; AState: TGridDrawState);
var
lCanvas: TCanvas;
begin
if Sender = StringGrid1 then
lCanvas := StringGrid1.Canvas
else if Sender = GridPrinter1 then
lCanvas := GridPrinter1.Canvas
else
raise Exception.Create('Unknown sender of OnPrepareCanvas.');
if ARow < StringGrid1.FixedRows then
lCanvas.Font.Style := [fsBold];
end;Printing a TDBGrid requires extra work because the grid only holds a small portion of the dataset. You need to provide event handlers for OnGetRowCount, OnBeforePrint, OnNewLine, and OnGetCellText. See the example projects (dbgrid2) in the GridPrinter installation folder for details.
Example snippets:
procedure TForm1.GridPrinter1GetRowCount(Sender: TObject; AGrid: TCustomGrid;
var ARowCount: Integer);
var
dbGrid: TDBGrid;
begin
dbGrid := AGrid as TDBGrid;
dbGrid.DataSource.DataSet.Last;
dbGrid.DataSource.DataSet.First;
ARowCount := dbGrid.DataSource.DataSet.RecordCount + 1; // +1 for header row
end;
procedure TForm1.GridPrinter1BeforePrint(Sender: TObject);
begin
DBGrid1.DataSource.DataSet.First;
end;
procedure TForm1.GridPrinter1NewLine(Sender: TObject; AGrid: TCustomGrid;
ARow: Integer);
var
dbGrid: TDBGrid;
begin
dbGrid := AGrid as TDBGrid;
BufDataset1.RecNo := ARow; // RecNo starts at 1
end;
procedure TForm1.GridPrinter1GetCellText(Sender: TObject; AGrid: TCustomGrid;
ACol, ARow: Integer; var AText: String);
var
dbGrid: TDBGrid;
col: TColumn;
colOffs: Integer;
begin
AText := '';
dbGrid := AGrid as TDBGrid;
if dgIndicator in dbGrid.Options then colOffs := 1 else colOffs := 0;
if ACol < colOffs then Exit;
col := dbGrid.Columns[ACol - colOffs];
if ARow = 0 then AText := col.FieldName
else AText := col.Field.AsString;
end;Enable the header or footer, set its Visible property to True, and enter text with the $PAGE symbol (e.g., '|$PAGE|' for centered page number). Other symbols: $PAGECOUNT, $DATE, $TIME, $FILENAME, etc.
function CreatePreviewBitmap(APageNo, APercentage: Integer): TBitmapfunction GetCellText(ACol, ARow: Integer): Stringprocedure Print– main methodprocedure ScaleToPages(NumHor, NumVert: Integer)function ScaleX(AValue: Integer): Integerfunction ScaleY(AValue: Integer): Integerprocedure UpdatePreview
Canvas: TCanvasColCount: IntegerColWidth[AIndex: Integer]: DoubleFooterMargin: IntegerHeaderMargin: IntegerPageHeight, PageWidth: IntegerPageRect: TRectPixelsPerInchX, PixelsPerInchY: IntegerPadding: IntegerPageCount: IntegerPrintPageNumber: IntegerPrintScaleToNumHorPages, PrintScaleToNumVertPages: IntegerPrintScalingMode: TGridPrnScalingModeRowCount: IntegerRowHeight[AIndex: Integer]: Double
Properties
Grid: TCustomGrid– link to the grid to print.BorderLineColor, BorderLineWidthFileName: String– used in header/footer symbols.FixedLineColor, FixedLineWidthFooter: TGridPrnHeaderFooterFromPage, ToPage: IntegerGridLineColor, GridLineWidthHeader: TGridPrnHeaderFooterMargins: TGridPrnMarginsMonochrome: BooleanOptions: TGridPrnOptions– includes centering, grid lines, borders, etc.Orientation: TPrinterOrientationPrintOrder: TGridPrnOrder– rows first or columns first.PrintScaleFactor: DoubleShowPrintDialog: TGridPrnDialog
Events
OnAfterPrint,OnBeforePrintOnGetCellText,OnGetRowCount,OnGetColCountOnLinePrinted,OnNewLine,OnNewPageOnPrepareCanvasOnPrintCellOnUpdatePreview
Class for header/footer lines.
Published properties
Font: TFontLineColor, LineWidthSectionSeparator: String(default '|')ShowLine: BooleanText: String– supports symbols:$DATE,$PAGECOUNT,$PAGE,$FULL_FILENAME,$FILENAME,$PATH,$TIMEVisible: Boolean
Public properties
ProcessedText[AIndex: TGridPrnHeaderFooterSection]: StringSectionText[AIndex: TGridPrnHeaderFooterSection]: String
Margin parameters (in millimeters):
Left, Top, Right, BottomHeader– margin to top of headerFooter– margin to bottom of footer
(In unit GridPrnPreviewDlg)
procedure Execute– main method.
FormParams: TGridPrintPreviewFormParamsGridPrinter: TGridPrinterOptions: TGridPrintPreviewOptions– controls visibility of toolbar elements (navigation, zoom, orientation, margins, header/footer setup, print order, centering, scaling, page setup).Zoom: Integer– scaling factor in percent (default 100).ZoomMode: TGridPrintPreviewZoomMode–zmCustom,zmFitWidth,zmFitHeight.
Left, Top, Width, Height: IntegerPosition: TPosition– e.g.,poMainFormCenter,poScreenCenter.
The preview dialog includes a toolbar with the following buttons (icons are taken from the Lazarus general-purpose image collection by Roland Hahn):
(Unit GridPrnActions)
When its GridPrinter property is linked to a TGridPrinter instance, and the action is assigned to a control’s Action property, clicking the control executes the Print method without any additional code.
When its PreviewDialog property is linked to a TGridPrintPreviewDialog instance, clicking the control runs the Execute method of the preview dialog.
This version of GridPrinter has been modified from the original by Werner Pamler.
Original:
- Author: Werner Pamler
- License: Modified LGPL-2 (with linking exception, like Lazarus LCL)
- Source: GridPrinter on Free Pascal wiki
Modifications:
- Modified for use in Notetask application (© 2024 Alexander Tverskoy)
- Enhancements:
- Enhanced tag printing support
These modifications are distributed under the same license as the original: Modified LGPL-2 with linking exception.
--
This README is based on the GridPrinter wiki page (last edited 14 December 2022).




















