diff --git a/components/gridprinter/examples/fpspreadsheet/main.pas b/components/gridprinter/examples/fpspreadsheet/main.pas index 1ba6da82c..52b3272c9 100644 --- a/components/gridprinter/examples/fpspreadsheet/main.pas +++ b/components/gridprinter/examples/fpspreadsheet/main.pas @@ -39,6 +39,7 @@ type ACanvas: TCanvas; ACol, ARow: Integer; ARect: TRect); private FGridCanvas: TCanvas; + FOldZoomFactor: Double; FOldPadding: Integer; FNewPadding: Integer; @@ -67,23 +68,15 @@ begin sWorkbookSource1.LoadFromSpreadsheetFile(ParamStr(1)); end; -procedure TMainForm.GridPrinter1AfterPrint(Sender: TObject); -begin - sWorksheetGrid1.Canvas := FGridCanvas; - varCellPadding := FOldPadding; - - // Restore drawing of the grid lines by the grid printer. - GridPrinter1.Options := GridPrinter1.Options + [ - gpoHorGridLines, gpoVertGridLines, - gpoFixedHorGridLines, gpoFixedVertGridLines, - gpoHeaderBorderLines - ]; -end; - procedure TMainForm.GridPrinter1BeforePrint(Sender: TObject); 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; + + // FOldPadding := varCellPadding; FNewPadding := GridPrinter1.Padding - varCellPadding; @@ -95,6 +88,28 @@ begin gpoFixedHorGridLines, gpoFixedVertGridLines, gpoHeaderBorderLines ]; + + // Store the worksheetgrid's ZoomFactor. We may have to change it + FOldZoomFactor := sWorksheetGrid1.ZoomFactor; + sWorksheetGrid1.ZoomFactor := GridPrinter1.PrintScaleFactor; +end; + +procedure TMainForm.GridPrinter1AfterPrint(Sender: TObject); +begin + // Restore the worksheetgrid's Canvas. + sWorksheetGrid1.Canvas := FGridCanvas; + + varCellPadding := FOldPadding; + + // Restore drawing of the grid lines by the grid printer. + GridPrinter1.Options := GridPrinter1.Options + [ + gpoHorGridLines, gpoVertGridLines, + gpoFixedHorGridLines, gpoFixedVertGridLines, + gpoHeaderBorderLines + ]; + + // Restore the worksheetgrid's ZoomFactor + sWorksheetGrid1.ZoomFactor := FOldZoomFactor; end; procedure TMainForm.btnPrintClick(Sender: TObject);