[SOLVED] GridPrinter with fpspreadsheet lazarus / 49-FPSpreadsheet ---------------------------------------------------------------------- rca · 2025-07-24 14:33 I am using GridPrinter (r9793) with fpspreadsheet. Windows 11 64 Bit Lazarus 4 and FPC fixes 3.2 I am attaching a simple example "testgprint.zip" based on "fpspreadsheet_demo". In Preview everything looks excellent. (Image attached) But when printing in PDF it presents some details: (Image attached) * Fixed's font is very small and is not legible. * The grid lines are almost not visible. * The text inside each cell is not aligned vertically in the center. * Float numbers are printed as integers. I appreciate the help you can give me. ---------------------------------------------------------------------- wp · 2025-07-24 22:38 I found some bugs in the TsWorksheetGrid and the GridPrinter. Please update your fpspreadsheet and your gridprinter packages from ccr: Use svn or download the zipped snapshots from https://sourceforge.net/p/lazarus- ccr/svn/HEAD/tree/components/fpspreadsheet/ and https://sourceforge.net/p/lazarus- ccr/svn/HEAD/tree/components/gridprinter/, respectively. With these changes, your test project needs to be updated at a few places - see corrected version in attachment ---------------------------------------------------------------------- rca · 2025-07-24 23:57 @wp Thank you for your work and dedication. I have already downloaded the latest versions of the components and the example that you have modified. Now when printing in PDF, it looks great (Image attached). However, it still shows the values as an integer and should be a float. Thanks for the help ---------------------------------------------------------------------- wp · 2025-07-25 00:40 Ah, since the worksheet grid displays numbers such that they are rounded to fit into a cell we must make sure that the column widths are scaled during printing to the higher printer resolution: Add an array FOldColWidths (array of integer) to the declarations of the main form: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TForm1 = class(TForm)  [...]  private    FGridCanvas: TCanvas;    FOldZoomFactor: Double;    FOldPadding: Integer;    FOldGridOptions: TGridOptions;    FOldColWidths: Array of Integer;  [...] Then modify the BeforePrint and AfterPrinter event handlers as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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; ---------------------------------------------------------------------- rca · 2025-07-25 00:57 I made the suggested changes and it works like a charm. (Image attached) Thank you so much for your explanation and help! ---------------------------------------------------------------------- wp · 2025-07-25 09:46 I added an adapter unit to the fpspreadsheet sample project which greatly simplifies printing of fpSpreadsheet grids: Just create an instance of TFPSpreadsheetGridPrinterAdapter and assign the instance of the GridPrinter to its GridPrinter property - this automatically creates the required event handlers. Likewise an adapter unit was added to simplify printing of DBGrids. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  ..., FPSpreadsheetPrintAdapter, ...; type  TMainForm = class(TForm)    [...]  private    FAdapter: TFPSpreadsheetGridPrinterAdapter;  [...]  end; procedure TMainForm.FormCreate(Sender: TObject);begin  // Use a TFPSpreadsheetGridPrinterAdapter to do all the adjustments needed  // for FPSpreadsheet grids.  FAdapter := TFPSpreadsheetGridPrinterAdapter.Create(self);  FAdapter.GridPrinter := GridPrinter1;   [...]end; See updated sample project in the examples/fpspreadsheet folder. ---------------------------------------------------------------------- rca · 2025-07-25 16:10 I installed the new version of Grid Printer by adding "fpspreadsheetprintadapter.pas", the adapter unit from the fpspreadsheet sample project, with the necessary changes (it simplifies the code), and it works like a charm. Thanks so much, @wp. Excellent work!.