procedure TForm1.GridPrinter1AfterPrint(Sender: TObject);
var
i: Integer;
begin
// Restore the worksheetgrid's Canvas.
sWorksheetGrid1.Canvas := FGridCanvas;
// Restore the orignal value of the CellPadding of the grid
varCellPadding := FOldPadding;
// Restore drawing of the grid lines in the worksheet grid
sWorksheetGrid1.Options := FOldGridOptions;
// Restore the original grid column widths
for i := 0 to High(FOldColWidths) do
sWorksheetGrid1.ColWidths[i] := FOldColWidths[i];
// Restore the worksheetgrid's ZoomFactor
sWorksheetGrid1.ZoomFactor := FOldZoomFactor;
end;
procedure TForm1.GridPrinter1BeforePrint(Sender: TObject);
var
i: Integer;
begin
// We want to draw on the printer/preview canvas. Therefore, we assign the
// grid's canvas to that of the GridPrinter. In order to restore the grid
// canvas after printing, we store its old canvas.
FGridCanvas := sWorksheetGrid1.Canvas;
sWorksheetGrid1.Canvas := GridPrinter1.Canvas;
// Since the cells are drawn by the grid we must make sure that the correctly
// scaled value of the CellPadding is used during printing.
FOldPadding := varCellPadding;
varCellPadding := GridPrinter1.Padding;
// The TsWorksheetGrid paints the grid lines in the DrawCell method. To
// avoid duplicate drawing (which, BTW, is offset by 1 pixel) we turn off
// painting of the grid lines in the worksheet grid so that the grid printer
// can take control. This is needed for correct scaling of the grid line width.
FOldGridOptions := sWorksheetGrid1.Options;
sWorksheetGrid1.Options := sWorksheetGrid1.Options - [
goHorzLine, goVertLine, goFixedHorzLine, goFixedVertLine
];
// Since the worksheet grid can reformat numeric cell values to fit into the
// cell widths we must scale the column widths to the printer resolution:
SetLength(FOldColWidths, sWorksheetGrid1.ColCount);
for i := 0 to High(FOldColWidths) do
begin
FOldColWidths[i] := sWorksheetGrid1.ColWidths[i];
sWorksheetGrid1.ColWidths[i] := GridPrinter1.ScaleX(FOldColWidths[i]);
end;
// Store the worksheetgrid's ZoomFactor. We may have to change it
FOldZoomFactor := sWorksheetGrid1.ZoomFactor;
sWorksheetGrid1.ZoomFactor := GridPrinter1.PrintScaleFactor;
end;