Skip to main content
Table of Contents

Printing

Send the grid to the system printer with TTMSFNCDataGridPrintIO — same options as the PDF exporter, just a different output device.

Populated DataGrid prepared for printed output Populated DataGrid prepared for printed output

Overview

TTMSFNCDataGridPrintIO mirrors TTMSFNCDataGridPDFIO but routes content to the system printer instead of writing a file. Drop the component, point DataGrid at the grid, configure Options, and call Print. The OS print dialog handles printer selection, page range, and copies.

Quick example

Send a DataGrid to the system printerPascalC++
procedure TForm1.FormCreate(Sender: TObject);
begin
  PrintIO.DataGrid := Grid;

  // Repeat fixed rows/columns on every printed page so each sheet has headers.
  PrintIO.Options.RepeatFixedRows    := True;
  PrintIO.Options.RepeatFixedColumns := True;

  // Default: scale the content to fit the printable page width.
  PrintIO.Options.FitToPage := True;
end;

procedure TForm1.PrintButtonClick(Sender: TObject);
begin
  PrintIO.Print;
end;
using namespace Fmx::Tmsfncdatagridprintio;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  PrintIO->DataGrid = Grid;

  // Repeat fixed rows/columns on every printed page so each sheet has headers.
  PrintIO->Options->RepeatFixedRows    = true;
  PrintIO->Options->RepeatFixedColumns = true;

  // Default: scale the content to fit the printable page width.
  PrintIO->Options->FitToPage = true;
}

void __fastcall TForm1::PrintButtonClick(TObject *Sender)
{
  PrintIO->Print();
}

Step by step

1. Wire the component

Inline example 1PascalC++
PrintIO.DataGrid := Grid;
PrintIO->DataGrid = Grid;

2. Configure scaling and headers

Option Default Effect
Scale 1.0 Uniform scale factor. Values below 1.0 shrink the output.
FitToPage True Fit grid width to the printable page width.
RepeatFixedRows False Repeat fixed rows as column headers on every printed page.
RepeatFixedColumns False Repeat fixed columns at the left margin of every printed page.
Header '' Static header text printed above the grid on every page.
Footer '' Static footer text printed below the grid on every page.
Inline example 2PascalC++
PrintIO.Scale           := 0.85;           // shrink to 85% to fit wide grids
PrintIO.FitToPage       := True;           // also fit width to page
PrintIO.RepeatFixedRows := True;           // column headers on every page
PrintIO.Header          := 'Sales Report — ' + FormatDateTime('yyyy-mm-dd', Now);
PrintIO.Footer          := 'Confidential';
PrintIO->Scale           = 0.85;           // shrink to 85% to fit wide grids
PrintIO->FitToPage       = true;           // also fit width to page
PrintIO->RepeatFixedRows = true;           // column headers on every page
PrintIO->Header          = L"Sales Report — " + FormatDateTime(L"yyyy-mm-dd", Now);
PrintIO->Footer          = L"Confidential";

3. Print

Inline example 3PascalC++
PrintIO.Print;
PrintIO->Print;

This brings up the OS print dialog. The user picks the printer, page range, and number of copies; the grid handles pagination, fitting, and rendering.

4. Dynamic page breaks

Dynamic page breaks when printingPascalC++
procedure TForm1.PrintIORowIsPageBreak(Sender: TObject;
  AExportObject: TTMSFNCPrintIOExportObject; ARow: Integer;
  var IsPageBreak: Boolean);
begin
  IsPageBreak := IsGroupHeader(ARow);     // your own predicate
end;
using namespace Fmx::Tmsfncdatagridprintio;
void __fastcall TForm1::PrintIORowIsPageBreak(TObject *Sender,
  TTMSFNCPrintIOExportObject *AExportObject, int ARow,
  bool &IsPageBreak)
{
  IsPageBreak = IsGroupHeader(ARow);     // your own predicate
}
Populated DataGrid prepared for printed output Populated DataGrid prepared for printed output

Build a preview workflow with the PDF exporter: write the grid to a temporary PDF, open it in the user's default PDF viewer, and let them print from there:

Inline example 4PascalC++
PDF.Options.OpenInPDFReader := True;
PDF.Save(TempFileName);
// User reviews in their default PDF viewer, then prints from there.
PDF->Options->OpenInPDFReader = true;
PDF->Save(TempFileName);
// User reviews in their default PDF viewer, then prints from there.

Combining print options and page breaks

Printed grids need two things to read well across pages: headers repeated on every sheet, and page breaks at logical boundaries instead of wherever a page happens to fill up. These settings work together, because the repeated fixed rows are what make a manual break start a readable new page. Combine them for grouped reports, such as one page per customer. The example below binds the print component, repeats fixed rows and columns, fits content to the page width, breaks the page before every row flagged as a section start, and prints.

Combining print options and page breaksPascalC++
procedure TForm1.FormCreate(Sender: TObject);
begin
  PrintIO.DataGrid := Grid;

  // Repeat the header on each page and fit the printed width
  PrintIO.Options.RepeatFixedRows := True;
  PrintIO.Options.RepeatFixedColumns := True;
  PrintIO.Options.FitToPage := True;

  // Break the page before rows flagged as section starts
  PrintIO.OnRowIsPageBreak := PrintIORowIsPageBreak;
end;

procedure TForm1.PrintIORowIsPageBreak(Sender: TObject;
  AExportObject: TTMSFNCPrintIOExportObject; ARow: Integer;
  var IsPageBreak: Boolean);
begin
  // Every tenth row starts a new section
  IsPageBreak := (ARow > 1) and ((ARow - 1) mod 10 = 0);
end;

procedure TForm1.PrintButtonClick(Sender: TObject);
begin
  PrintIO.Print;
end;
using namespace Fmx::Tmsfncdatagridprintio;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  PrintIO->DataGrid = Grid;

  // Repeat the header on each page and fit the printed width
  PrintIO->Options->RepeatFixedRows = true;
  PrintIO->Options->RepeatFixedColumns = true;
  PrintIO->Options->FitToPage = true;

  // Break the page before rows flagged as section starts
  PrintIO->OnRowIsPageBreak = PrintIORowIsPageBreak;
}

void __fastcall TForm1::PrintIORowIsPageBreak(TObject *Sender,
  TTMSFNCPrintIOExportObject *AExportObject, int ARow,
  bool &IsPageBreak)
{
  // Every tenth row starts a new section
  IsPageBreak = (ARow > 1) && ((ARow - 1) % 10 == 0);
}

void __fastcall TForm1::PrintButtonClick(TObject *Sender)
{
  PrintIO->Print();
}
  • TTMSFNCDataGridPrintIO
  • PrintIO.Print
  • PrintIO.Options.Scale / FitToPage
  • PrintIO.Options.RepeatFixedRows / RepeatFixedColumns
  • PrintIO.Options.Header / Footer
  • OnRowIsPageBreak
  • OnBeforeDrawContent, OnAfterDrawContent
  • OnGetHeader, OnGetFooter

See also

  • PDF export — same options, file-based output with preview.
  • Demo: Demo/FMX/DataGrid/Basic/PDF Export (illustrates the printing path too).