Forum > Beginners

SAVE StringGrid to PDF

<< < (2/7) > >>

dseligo:

--- Quote from: 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..

--- End quote ---

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.

seghele0:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;{$mode objfpc}{$H+}interfaceuses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids,  fpPDF;type  { TForm1 }  TForm1 = class(TForm)    Button1: TButton;    StringGrid1: TStringGrid;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private      procedure SaveToPDF(const FileName: string);  public  end;var  Form1: TForm1;implementation{$R *.lfm}{ TForm1 }procedure TForm1.Button1Click(Sender: TObject);var  SaveDialog: TSaveDialog;begin  SaveDialog := TSaveDialog.Create(Self);  try    SaveDialog.Filter := 'PDF Files|*.pdf';    SaveDialog.DefaultExt := 'pdf';    if SaveDialog.Execute then    begin      SaveToPDF(SaveDialog.FileName);    end;  finally    SaveDialog.Free;  end;end; procedure TForm1.FormCreate(Sender: TObject);begin  with StringGrid1 do  begin    ColCount := 5;    RowCount := 3;    Cells[0, 0] := '1a'; Cells[1, 0] := '2a'; Cells[2, 0] := '3a';    Cells[3, 0] := '4a'; Cells[4, 0] := '5a';    Cells[0, 1] := '1b'; Cells[1, 1] := '2b'; Cells[2, 1] := '3b';    Cells[3, 1] := '4b'; Cells[4, 1] := '5b';  end;end; procedure TForm1.SaveToPDF(const FileName: string);var  PDF: TPDFDocument;  Page: TPDFPage;  i, j: Integer;  YPos: Double;begin  PDF := TPDFDocument.Create;  try    PDF.AddPage;    Page := PDF.Pages[0];    YPos := Page.Height - 50;    for i := 0 to StringGrid1.RowCount - 1 do    begin      for j := 0 to StringGrid1.ColCount - 1 do      begin        Page.WriteText(50 + j * 100, YPos, StringGrid1.Cells[j, i]);      end;      YPos := YPos - 20;    end;    PDF.SaveToFile(FileName);  finally    PDF.Free;  end;end;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"
--- End quote ---
;)

dseligo:
This is not complete example project. Make a zip with complete example project (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.


--- Quote from: seghele0 on December 07, 2024, 05:15:01 pm ---unit1.pas(63,23) Error: Wrong number of parameters specified for call to "Create"
--- End quote ---

Change line 60 to:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---PDF := TPDFDocument.Create(nil);

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

Change line 62 and 63 to:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Page := PDF.Pages.AddPage;

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

Change line 64 to:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---YPos := Page.GetPaperHeight - 50;

cdbc:
Hi
Here's my take on it, at least it prints something to the pdf...
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids,  fpPDF; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    StringGrid1: TStringGrid;    procedure Button1Click(Sender: TObject);  private    procedure SaveToPDF(const FileName: string);  public    procedure AfterConstruction; override;    procedure BeforeDestruction; override;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  SaveDialog: TSaveDialog;begin  SaveDialog := TSaveDialog.Create(Self);  try    SaveDialog.Filter := 'PDF Files|*.pdf';    SaveDialog.DefaultExt := 'pdf';    if SaveDialog.Execute then    begin      SaveToPDF(SaveDialog.FileName);    end;  finally    SaveDialog.Free;  end;end; procedure TForm1.SaveToPDF(const FileName: string);var  PDF: TPDFDocument;   S:TPdfSection;  Page: TPDFPage;  i, j, FontHelv: Integer;  YPos: ptrint;begin  PDF := TPDFDocument.Create(nil);  try    PDF.StartDocument;    FontHelv:= PDF.AddFont('Helvetica');     // One of the "14 standard PDF Fonts", does not need a path or name    S:= PDF.Sections.AddSection;     Page:= PDF.Pages.AddPage;    Page.PaperType:= ptA4;    Page.Orientation:= ppoPortrait;    Page.UnitOfMeasure:= uomMillimeters;    S.AddPage(Page);    Page.SetFont(FontHelv, 12);    Page.SetColor(clBlue,false);    YPos:= 20; //Page.Paper.H - 15;    for i := 1 to StringGrid1.RowCount - 1 do    begin      for j := 0 to StringGrid1.ColCount - 1 do      begin        Page.WriteText(15 + j * 10, YPos, StringGrid1.Cells[j, i]);      end;      inc(YPos,20);    end;    PDF.SaveToFile(FileName);  finally    PDF.Free;  end;end; procedure TForm1.AfterConstruction;begin  inherited AfterConstruction;  with StringGrid1 do begin    ColCount := 5;    RowCount := 3;    Cells[0, 1] := '1a'; Cells[1, 1] := '2a'; Cells[2, 1] := '3a';    Cells[3, 1] := '4a'; Cells[4, 1] := '5a';    Cells[0, 2] := '1b'; Cells[1, 2] := '2b'; Cells[2, 2] := '3b';    Cells[3, 2] := '4b'; Cells[4, 2] := '5b';  end;end; procedure TForm1.BeforeDestruction;begin   inherited BeforeDestruction;end; end. This won't save you... You *need* to study this subject!!!
Regards Benny

seghele0:
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.
 ;)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version