Lazarus

Free Pascal => Beginners => Topic started by: seghele0 on December 06, 2024, 06:27:02 pm

Title: SAVE StringGrid to PDF
Post by: seghele0 on December 06, 2024, 06:27:02 pm
The procedures below work well.
But I'm trying to export the Stringgrid to a “PDF” file to set the font size to Dejavu sans mono 15pt.
Can someone please give me the code?
The 'PowerPDF' component is already installed.
Thanks already.
 ;)
Code: Pascal  [Select][+][-]
  1. //  SAVE StringGrid TO TXT
  2. procedure SAVEStringGridTXT(StringGrid1: TStringGrid);
  3. var
  4.   varScoreBestandsnaam: String;
  5.   varCellLen: Integer;
  6.   varLijn: String;
  7.   varRow: Integer;
  8.   varCol: Integer;
  9.   varFile1: TextFile;
  10.   tmpValue: Single;
  11.   tmpColCountArr: array of Integer;   // col length array
  12.   PageWidth: Integer;
  13. begin
  14.   // Show vertical and horizontal lines
  15.   StringGrid1.Options := StringGrid1.Options + [goVertLine, goHorzLine];
  16.   // Adjust column widths automatically
  17.   StringGrid1.AutoSizeColumns;
  18.   // Check max length for each column
  19.   if StringGrid1.ColCount > 0 then
  20.     SetLength(tmpColCountArr, StringGrid1.ColCount)
  21.   else
  22.     raise Exception.Create('PROBLEM !');
  23.   for varCol := 0 to StringGrid1.ColCount - 1 do
  24.   begin
  25.     for varRow := 0 to StringGrid1.RowCount - 1 do
  26.     begin
  27.       varCellLen := Length(StringGrid1.Cells[varCol, varRow]);
  28.       if varRow = 0 then
  29.         tmpColCountArr[varCol] := varCellLen
  30.       else if tmpColCountArr[varCol] < varCellLen then
  31.         tmpColCountArr[varCol] := varCellLen;
  32.     end;
  33.   end;
  34.   // Calculate page width
  35.   PageWidth := 0;
  36.   for varCol := 0 to High(tmpColCountArr) do
  37.     PageWidth := PageWidth + tmpColCountArr[varCol] + 3; // Kolombreedtes + spaties
  38.   PageWidth := PageWidth + 2; // For the lines at the beginning/end of each row
  39.   varScoreBestandsnaam := 'WIEZEN.txt';
  40.   // Write to the file
  41.   AssignFile(varFile1, varScoreBestandsnaam);
  42.   try
  43.     Rewrite(varFile1);
  44.     // Write headline
  45.     varLijn := StringOfChar(' ', (PageWidth - Length('WIEZEN.txt')) div 2) + 'WIEZEN.txt';
  46.     Writeln(varFile1, varLijn);
  47.     Writeln(varFile1, StringOfChar('-', PageWidth));
  48.     Writeln(varFile1, 'Resultaat Wiezen - ' + FormatDateTime('dd/mm/yyyy HH:nn', Now));
  49.     Writeln(varFile1, StringOfChar('-', PageWidth));
  50.     // Write data
  51.     for varRow := 0 to StringGrid1.RowCount - 1 do
  52.     begin
  53.       varLijn := '|';
  54.       for varCol := 0 to StringGrid1.ColCount - 1 do
  55.       begin
  56.         if TryStrToFloat(StringGrid1.Cells[varCol, varRow], tmpValue) then
  57.           // Align numerical values to the "right"
  58.           varLijn := varLijn + ' ' + Format('%' + IntToStr(tmpColCountArr[varCol]) +
  59.             's', [FormatFloat('0.####', tmpValue)]) + ' |'
  60.         else
  61.           // Align text values to the "right"
  62.           varLijn := varLijn + ' ' + Format('%' + IntToStr(tmpColCountArr[varCol]) +
  63.             's', [StringGrid1.Cells[varCol, varRow]]) + ' |';
  64.       end;
  65.       Writeln(varFile1, varLijn);
  66.     end;
  67.     // Footer writing
  68.     Writeln(varFile1, StringOfChar('-', PageWidth));
  69.   finally
  70.     CloseFile(varFile1);
  71.   end;
  72. end;      
  73. *****************************
  74. // SAVE the files "txt" and "xlsx".
  75. procedure TFmain.SBUTTONSAVEClick(Sender: TObject);
  76. begin
  77.   SAVEStringGridTXT(StringGrid1);  //execute procedure
  78. end;                          
*****************************
Via Google I got the code below, but apparently this doesn't work, starting with uses:  fpdf.

Code: Pascal  [Select][+][-]
  1. uses
  2.   FPReport, FPCanvas, FPImage, fpdf;
  3.  
  4. procedure BewaarStringGridPDF(StringGrid1: TStringGrid);
  5. var
  6.   PDF: TPDFDocument;
  7.   Page: TPDFPage;
  8.   Font: TPDFFont;
  9.   x, y, i, j: Integer;
  10.   CellText: String;
  11.   CellWidth, CellHeight: Double;
  12.   ColWidths: array of Double;
  13. begin
  14.   PDF := TPDFDocument.Create;
  15.   try
  16.     PDF.Info.Title := 'Wiezen Resultaten';
  17.     PDF.StartDocument;
  18.     // Nieuwe pagina
  19.     Page := PDF.AddPage;
  20.     Page.Height := 842; // A4 Hoogte in punten
  21.     Page.Width := 595;  // A4 Breedte in punten
  22.     // Lettertype instellen
  23.     Font := Page.AddFont('DejaVu Sans Mono', fpdaSubset);
  24.     Font.Size := 16; // Lettergrootte 16 pt
  25.     Page.SetFont(Font);
  26.     // Marges en startpositie
  27.     x := 50; // Begin van links
  28.     y := 780; // Begin van boven
  29.     // Kolombreedtes berekenen
  30.     SetLength(ColWidths, StringGrid1.ColCount);
  31.     for i := 0 to StringGrid1.ColCount - 1 do
  32.     begin
  33.       ColWidths[i] := 100; // Standaard kolombreedte
  34.       for j := 0 to StringGrid1.RowCount - 1 do
  35.       begin
  36.         if Length(StringGrid1.Cells[i, j]) * Font.Size / 2 > ColWidths[i] then
  37.           ColWidths[i] := Length(StringGrid1.Cells[i, j]) * Font.Size / 2;
  38.       end;
  39.     end;
  40.     // Celhoogte instellen
  41.     CellHeight := 20;
  42.     // Header schrijven
  43.     for i := 0 to StringGrid1.ColCount - 1 do
  44.     begin
  45.       CellText := StringGrid1.Cells[i, 0];
  46.       Page.DrawText(x, y, CellText);
  47.       x := x + Round(ColWidths[i]);
  48.     end;
  49.     x := 50;
  50.     y := y - Round(CellHeight); // Naar beneden
  51.     // Gegevens schrijven
  52.     for j := 1 to StringGrid1.RowCount - 1 do
  53.     begin
  54.       for i := 0 to StringGrid1.ColCount - 1 do
  55.       begin
  56.         CellText := StringGrid1.Cells[i, j];
  57.         Page.DrawText(x, y, CellText);
  58.         x := x + Round(ColWidths[i]);
  59.       end;
  60.       x := 50;
  61.       y := y - Round(CellHeight); // Naar beneden
  62.     end;
  63.     PDF.SaveToFile('WIEZEN.pdf');
  64.   finally
  65.     PDF.Free;
  66.   end;
  67. end;

Title: Re: SAVE StringGrid to PDF
Post by: Fred vS on December 06, 2024, 10:06:07 pm
Via Google I got the code below, but apparently this doesn't work, starting with uses:  fpdf.

Hello.
Shouldn't it be fppdf instead?

Code: Pascal  [Select][+][-]
  1. uses
  2. ..., fppdf;


Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 07, 2024, 02:06:19 pm
OK, I've changed into fppdf.
But, there are many errors:
Quote
Compile Project, Target: C:\Lazarus3\LAZARUSPROGS\WIEZEN-20241206\WIEZEN.exe: Exit code 1, Errors: 16
umain.pas(189,26) Error: Incompatible type for arg no. 1: Got "TFPReportVertTextAlignment", expected "TTextLayout"
umain.pas(316,23) Error: Wrong number of parameters specified for call to "Create"
fppdf.pp(5683,26) Error: Found declaration: constructor Create(TComponent);
umain.pas(318,9) Error: identifier idents no member "Info"
umain.pas(321,17) Error: identifier idents no member "AddPage"
umain.pas(322,10) Error: identifier idents no member "Height"
umain.pas(323,10) Error: identifier idents no member "Width"
umain.pas(325,18) Error: identifier idents no member "AddFont"
umain.pas(325,46) Error: Identifier not found "fpdaSubset"
umain.pas(326,10) Error: identifier idents no member "Size"
umain.pas(327,23) Error: Wrong number of parameters specified for call to "SetFont"
fppdf.pp(2346,20) Error: Found declaration: SetFont(LongInt;LongInt);
umain.pas(338,51) Error: identifier idents no member "Size"
umain.pas(339,66) Error: identifier idents no member "Size"
umain.pas(348,12) Error: identifier idents no member "DrawText"
umain.pas(359,14) Error: identifier idents no member "DrawText"
Title: Re: SAVE StringGrid to PDF
Post by: Fred vS on December 07, 2024, 02:55:01 pm
Quote
OK, I've changed into fppdf.
But, there are many errors:

You said that Google gives the code...
Hum, was it from Google AI ?
If so, not very impressing.

Maybe try with a other AI (Chatgpt, ...).
(Or even better, study the fppdf code https://wiki.freepascal.org/fcl-pdf  :-X )
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 07, 2024, 03:42:54 pm
I did the maximum, but my level of programming is too low to make this complex procedure.
If no help is possible, which I can understand, then I drop the problem.
I've a working procedure for a stringgrid to TXT, but would have liked to have the PDF alternative for hand.
 :-[
Thanks for the answers..

Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 07, 2024, 04:09:05 pm
I did the maximum, but my level of programming is too low to make this complex procedure.
If no help is possible, which I can understand, then I drop the problem.
I've a working procedure for a stringgrid to TXT, but would have liked to have the PDF alternative for hand.
 :-[
Thanks for the answers..

I suggest you make complete example project which shows your problem and upload it here. It will significantly raise chances that some one will look at your problem and help you.
You posted two code snippets of 145 lines total length. Your first error is at line 189 and further errors are at lines 316 and above. While there is a chance that some will catch errors in your code snippets, it is much easier to open project in Lazarus and try to compile and test it.
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 07, 2024, 05:15:01 pm
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids,
  6.   fpPDF;
  7. type
  8.   { TForm1 }
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     StringGrid1: TStringGrid;
  12.     procedure Button1Click(Sender: TObject);
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.       procedure SaveToPDF(const FileName: string);
  16.   public
  17.   end;
  18. var
  19.   Form1: TForm1;
  20. implementation
  21. {$R *.lfm}
  22. { TForm1 }
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. var
  25.   SaveDialog: TSaveDialog;
  26. begin
  27.   SaveDialog := TSaveDialog.Create(Self);
  28.   try
  29.     SaveDialog.Filter := 'PDF Files|*.pdf';
  30.     SaveDialog.DefaultExt := 'pdf';
  31.     if SaveDialog.Execute then
  32.     begin
  33.       SaveToPDF(SaveDialog.FileName);
  34.     end;
  35.   finally
  36.     SaveDialog.Free;
  37.   end;
  38. end;
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   with StringGrid1 do
  43.   begin
  44.     ColCount := 5;
  45.     RowCount := 3;
  46.     Cells[0, 0] := '1a'; Cells[1, 0] := '2a'; Cells[2, 0] := '3a';
  47.     Cells[3, 0] := '4a'; Cells[4, 0] := '5a';
  48.     Cells[0, 1] := '1b'; Cells[1, 1] := '2b'; Cells[2, 1] := '3b';
  49.     Cells[3, 1] := '4b'; Cells[4, 1] := '5b';
  50.   end;
  51. end;
  52.  
  53. procedure TForm1.SaveToPDF(const FileName: string);
  54. var
  55.   PDF: TPDFDocument;
  56.   Page: TPDFPage;
  57.   i, j: Integer;
  58.   YPos: Double;
  59. begin
  60.   PDF := TPDFDocument.Create;
  61.   try
  62.     PDF.AddPage;
  63.     Page := PDF.Pages[0];
  64.     YPos := Page.Height - 50;
  65.     for i := 0 to StringGrid1.RowCount - 1 do
  66.     begin
  67.       for j := 0 to StringGrid1.ColCount - 1 do
  68.       begin
  69.         Page.WriteText(50 + j * 100, YPos, StringGrid1.Cells[j, i]);
  70.       end;
  71.       YPos := YPos - 20;
  72.     end;
  73.     PDF.SaveToFile(FileName);
  74.   finally
  75.     PDF.Free;
  76.   end;
  77. end;
  78. end.                  

ERRORS:
Quote
Compile Project, Target: C:\Lazarus3\LAZARUSPROGS\TEST-PDF\project1.exe: Exit code 1, Errors: 4
unit1.pas(63,23) Error: Wrong number of parameters specified for call to "Create"
fppdf.pp(5683,26) Error: Found declaration: constructor Create(TComponent);
unit1.pas(65,9) Error: identifier idents no member "AddPage"
unit1.pas(67,18) Error: identifier idents no member "Height"
;)
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 07, 2024, 05:56:11 pm
This is not complete example project. Make a zip with complete example project (https://wiki.freepascal.org/Forum#Sharing_large_pieces_of_code (https://wiki.freepascal.org/Forum#Sharing_large_pieces_of_code)) which anyone can extract and compile.

Just to show you we can help you, I will correct code snippet you posted. But I won't help you if you'll have further problems unless you make what I asked you to do. You see, even line numbers you got and posted here doesn't match snipped you sent. It's hard to correct multiple errors like this. And I am sure I could give you couple of suggestions if I had example project to play with.

unit1.pas(63,23) Error: Wrong number of parameters specified for call to "Create"

Change line 60 to:
Code: Pascal  [Select][+][-]
  1. PDF := TPDFDocument.Create(nil);

Quote
unit1.pas(65,9) Error: identifier idents no member "AddPage"

Change line 62 and 63 to:
Code: Pascal  [Select][+][-]
  1. Page := PDF.Pages.AddPage;

Quote
unit1.pas(67,18) Error: identifier idents no member "Height"

Change line 64 to:
Code: Pascal  [Select][+][-]
  1. YPos := Page.GetPaperHeight - 50;
Title: Re: SAVE StringGrid to PDF
Post by: cdbc on December 07, 2024, 06:52:33 pm
Hi
Here's my take on it, at least it prints something to the pdf...
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids,
  9.   fpPDF;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     StringGrid1: TStringGrid;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.     procedure SaveToPDF(const FileName: string);
  21.   public
  22.     procedure AfterConstruction; override;
  23.     procedure BeforeDestruction; override;
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. var
  37.   SaveDialog: TSaveDialog;
  38. begin
  39.   SaveDialog := TSaveDialog.Create(Self);
  40.   try
  41.     SaveDialog.Filter := 'PDF Files|*.pdf';
  42.     SaveDialog.DefaultExt := 'pdf';
  43.     if SaveDialog.Execute then
  44.     begin
  45.       SaveToPDF(SaveDialog.FileName);
  46.     end;
  47.   finally
  48.     SaveDialog.Free;
  49.   end;
  50. end;
  51.  
  52. procedure TForm1.SaveToPDF(const FileName: string);
  53. var
  54.   PDF: TPDFDocument;
  55.   S:TPdfSection;
  56.   Page: TPDFPage;
  57.   i, j, FontHelv: Integer;
  58.   YPos: ptrint;
  59. begin
  60.   PDF := TPDFDocument.Create(nil);
  61.   try
  62.     PDF.StartDocument;
  63.     FontHelv:= PDF.AddFont('Helvetica');     // One of the "14 standard PDF Fonts", does not need a path or name
  64.     S:= PDF.Sections.AddSection;
  65.     Page:= PDF.Pages.AddPage;
  66.     Page.PaperType:= ptA4;
  67.     Page.Orientation:= ppoPortrait;
  68.     Page.UnitOfMeasure:= uomMillimeters;
  69.     S.AddPage(Page);
  70.     Page.SetFont(FontHelv, 12);
  71.     Page.SetColor(clBlue,false);
  72.     YPos:= 20; //Page.Paper.H - 15;
  73.     for i := 1 to StringGrid1.RowCount - 1 do
  74.     begin
  75.       for j := 0 to StringGrid1.ColCount - 1 do
  76.       begin
  77.         Page.WriteText(15 + j * 10, YPos, StringGrid1.Cells[j, i]);
  78.       end;
  79.       inc(YPos,20);
  80.     end;
  81.     PDF.SaveToFile(FileName);
  82.   finally
  83.     PDF.Free;
  84.   end;
  85. end;
  86.  
  87. procedure TForm1.AfterConstruction;
  88. begin
  89.   inherited AfterConstruction;
  90.   with StringGrid1 do begin
  91.     ColCount := 5;
  92.     RowCount := 3;
  93.     Cells[0, 1] := '1a'; Cells[1, 1] := '2a'; Cells[2, 1] := '3a';
  94.     Cells[3, 1] := '4a'; Cells[4, 1] := '5a';
  95.     Cells[0, 2] := '1b'; Cells[1, 2] := '2b'; Cells[2, 2] := '3b';
  96.     Cells[3, 2] := '4b'; Cells[4, 2] := '5b';
  97.   end;
  98. end;
  99.  
  100. procedure TForm1.BeforeDestruction;
  101. begin
  102.  
  103.   inherited BeforeDestruction;
  104. end;
  105.  
  106. end.
  107.  
This won't save you... You *need* to study this subject!!!
Regards Benny
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 08, 2024, 01:48:50 pm
dseligo
Thanks for replying.
I'll show you the 'zip' file.
Thank you.
 ;)
 cdbc
Thank you already, but I'll wait a little longer to test your code.
 ;)
Title: Re: SAVE StringGrid to PDF
Post by: cdbc on December 08, 2024, 03:15:32 pm
Hi
I just changed your code enough to make it work(ish)...
Regards Benny
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 08, 2024, 04:53:16 pm
Thank you.
Check out the new 'zip' with custom code.
It should be assumed that the string grid is already filled in with data and all the values in the grid must be exported to PDF.

For the font you use Helvetica, but is that also possible for DejaVu sans mono with a letter height of 15pt?

With the code in the 'ZIP', the PDF remains empty.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids,
  6.   fpPDF;
  7. type
  8.   { TForm1 }
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     StringGrid1: TStringGrid;
  12.     procedure Button1Click(Sender: TObject);
  13.   private
  14.       procedure SaveToPDF(const FileName: string);
  15.   public
  16.   end;
  17. var
  18.   Form1: TForm1;
  19.  
  20. implementation
  21. {$R *.lfm}
  22. { TForm1 }
  23.  
  24. procedure TForm1.Button1Click(Sender: TObject);
  25. var
  26.   SaveDialog: TSaveDialog;
  27. begin
  28.   SaveDialog := TSaveDialog.Create(Self);
  29.   try
  30.     SaveDialog.Filter := 'PDF Files|*.pdf';
  31.     SaveDialog.DefaultExt := 'pdf';
  32.     if SaveDialog.Execute then
  33.     begin
  34.       SaveToPDF(SaveDialog.FileName);
  35.     end;
  36.   finally
  37.     SaveDialog.Free;
  38.   end;
  39. end;
  40.  
  41. procedure TForm1.SaveToPDF(const FileName: string);
  42. var
  43.   PDF: TPDFDocument;
  44.   S: TPdfSection;
  45.   Page: TPDFPage;
  46.   i, j, FontHelv: Integer;
  47.   YPos: ptrint;
  48. begin
  49.   PDF := TPDFDocument.Create(nil);
  50.   try
  51.     PDF.StartDocument;
  52.     FontHelv:= PDF.AddFont('Helvetica');     // One of the "14 standard PDF Fonts", does not need a path or name
  53.     S:= PDF.Sections.AddSection;
  54.     Page:= PDF.Pages.AddPage;
  55.     Page.PaperType:= ptA4;
  56.     Page.Orientation:= ppoPortrait;
  57.     Page.UnitOfMeasure:= uomMillimeters;
  58.     S.AddPage(Page);
  59.     Page.SetFont(FontHelv, 12);
  60.     Page.SetColor(clBlue,false);
  61.     YPos:= 20; //Page.Paper.H - 15;
  62.     for i := 1 to StringGrid1.RowCount - 1 do
  63.     begin
  64.       for j := 0 to StringGrid1.ColCount - 1 do
  65.       begin
  66.         Page.WriteText(15 + j * 10, YPos, StringGrid1.Cells[j, i]);
  67.       end;
  68.       inc(YPos,20);
  69.     end;
  70.     PDF.SaveToFile(FileName);
  71.   finally
  72.     PDF.Free;
  73.   end;
  74. end;
  75. end.                          
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 08, 2024, 06:37:39 pm
With the code in the 'ZIP', the PDF remains empty.

Check project I attached. I attached created PDF file too.
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 09, 2024, 04:34:58 pm
Most thanks for the code, it works.
The 'Button1Click' is then no longer necessary.
Another small problem, I sometimes use the application on a Linux platform (Mint) by using "Wine". The 'exe' works fine via 'wine' but the font selection is a problem.
Maybe it is better not to encode a imposed font, but just the size of 15pt.
Is this possible?
 ;)
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 09, 2024, 04:41:35 pm
Sorry, I forgot to mention that the 'Headers' should also be shown on the PDF.
 ::)
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 09, 2024, 06:15:08 pm
Attached is the latest updated version.
The header is still missing.
Thanks in advance.
 ;)
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 09, 2024, 07:42:30 pm
Another small problem, I sometimes use the application on a Linux platform (Mint) by using "Wine". The 'exe' works fine via 'wine' but the font selection is a problem.
Maybe it is better not to encode a imposed font, but just the size of 15pt.
Is this possible?
 ;)

This code should compile under Linux, so you don't have to use Wine. You could also ship font you need with your program, and use it so you have same PDF everywhere.

Quote
Sorry, I forgot to mention that the 'Headers' should also be shown on the PDF.

In line 55 you start for loop with 1. Rows in StringGrid are zero based, so change line 55 to:
Code: Pascal  [Select][+][-]
  1.     for i := 0 to StringGrid1.RowCount - 1 do
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 10, 2024, 02:41:16 pm
Thanks for the quick tip.
I know I'm pushing for improvements to the code, but at 75 years old, things slow down.
As you can see, the PDF (attachment) is not really readable. The columns are too close to each other. Can this be adjusted?
 ;)
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 10, 2024, 02:51:05 pm
Thanks for the quick tip.
I know I'm pushing for improvements to the code, but at 75 years old, things slow down.

No problem. :)

Quote
As you can see, the PDF (attachment) is not really readable. The columns are too close to each other. Can this be adjusted?
 ;)

In line 60 of last project you sent is where you write text out. You control X (horizontal) position with expression 15 + j * 10. 15 is fixed offset from left side, j is column you write and then you multiply j with 10. Increase this multiplier and you will get wider columns.
I.e.:
Code: Pascal  [Select][+][-]
  1.         Page.WriteText(15 + j * 20, YPos, StringGrid1.Cells[j, i]);
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 10, 2024, 03:24:43 pm
Incredible and efficient.
Your kind support is an attempt to make the world a better place.
 :)
Thank you all.
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 10, 2024, 04:13:22 pm
Your kind support is an attempt to make the world a better place.

LOL, I wish it is that simple.

I wanted to mention earlier, but forgot: maybe you already know, but you could also use LazReport (https://wiki.freepascal.org/LazReport_Tutorial (https://wiki.freepascal.org/LazReport_Tutorial)) or FPReport (https://wiki.freepascal.org/FPReport_Designer (https://wiki.freepascal.org/FPReport_Designer)) to design documents and from them create PDF files.
Benefit is that you can visually design documents and this is great when documents become more complex. Also, you can print it directly and export it to other formats. But it also involves learning how to use these components.
Title: Re: SAVE StringGrid to PDF
Post by: jianwt on December 11, 2024, 01:41:29 am
@dseligo. When exporting PDF, can I export StringGrid horizontal and vertical form lines as well?
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 11, 2024, 02:40:40 am
@dseligo. When exporting PDF, can I export StringGrid horizontal and vertical form lines as well?

Well, you have DrawLine and DrawRect methods in TPDFPage, but you have to calculate coordinates.
I don't know of a simple way to do it like this. That is why, in reply #20, I mentioned that LazReport or FPReport (and probably some other components too) can be used when documents become more complex. Manually creating PDF can quickly become tedious.
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 13, 2024, 04:32:25 pm
I try to use LazReport as little as possible.
The code you all provided is great.
An additional procedure was added.
There is another request for a line under the heading of the stringgrid, but I get ERROR.
"Line 37: Identifier idents no member "Title".
I hope for a solution.
Thanks.

Title: Re: SAVE StringGrid to PDF
Post by: cdbc on December 13, 2024, 04:38:48 pm
Hi
Try this:
Code: Pascal  [Select][+][-]
  1.   // Get the bottom position of the StringGrid heading
  2.   Y1 := StringGrid1.Top + StringGrid1.Row[0].Height;
Regards Benny
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 13, 2024, 05:01:59 pm
Still ERROR:
Quote
Exit code 1, Errors: 2
unit1.pas(37,43) Error: Illegal qualifier
unit1.pas(37,45) Error: Syntax error, ";" expected but "." found
Code: Pascal  [Select][+][-]
  1.   Y1 := StringGrid1.Top + StringGrid1.Row[0].Height;  

Title: Re: SAVE StringGrid to PDF
Post by: cdbc on December 13, 2024, 05:06:00 pm
Then go have a looksee in the implementation of TStringgrid.
"Use the Source Luke"
/bc
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 13, 2024, 05:19:19 pm
 :o
Sorry, but I don't get it.
English is not my native language.
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 14, 2024, 05:12:59 pm
Try this:
Code: Pascal  [Select][+][-]
  1. Y1 := StringGrid1.Top + StringGrid1.RowHeights[0];
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 14, 2024, 05:36:14 pm
Thanks for your reply0
No line is drawn in the PDF.
On the Form there is a new little diagonal line, top left.
The code is still not correct.
 ;)
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 14, 2024, 06:50:20 pm
I was just correcting your compiling error.

In procedure DrawLineUnderHeading you draw on your screen canvas, not on the PDF.

Add this above For loop in SaveToPDF procedure:
Code: Pascal  [Select][+][-]
  1. Page.DrawLine(15, YPos, 15 + StringGrid1.ColCount * 20, YPos, 1);
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 15, 2024, 01:14:48 pm
Thanks for the code.
You can view the result in attachment.
After opening the PDF, a diagonal line is applied to the top left of the Form.
 ::)
Title: Re: SAVE StringGrid to PDF
Post by: dseligo on December 15, 2024, 01:35:54 pm
Thanks for the code.

You're welcome

Quote
After opening the PDF, a diagonal line is applied to the top left of the Form.

You draw that line in procedure DrawLineUnderHeading. Don't call this procedure if you don't want that line.
Title: Re: SAVE StringGrid to PDF
Post by: seghele0 on December 15, 2024, 01:47:35 pm
Attached I leave you the test application, with custom code.
Finally got hold of the code.
Thank you all and hopefully this is an instructive contribution for non-professional progrtammeurs.
 ;)
Code: Pascal  [Select][+][-]
  1.    // Draw line below header
  2.     YPos := YPos + HeaderHeight;
  3.     Page.DrawLine(15, YPos, 30 + (StringGrid1.ColCount - 1) * 20, YPos, LineThickness);    

TinyPortal © 2005-2018