Forum > Beginners

<Solved>Easy printing TMemo in my case Memo1.Lines

(1/2) > >>

Sprek Skik:
https://forum.lazarus.freepascal.org/index.php/topic,8334.msg40044.html#msg40044

Is an example how to print in my case Memo1.Lines

here is the code:


--- 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";}};} ---procedure TfrmPrinterSetUp.PrintTStrings(Lst : TStrings) ;var  I,  Line : Integer;begin  I := 0;  Line := 0 ;  Printer.BeginDoc ;  showmessage(IntToStr(Lst.Count-1));  for I := 0 to Lst.Count - 1 do begin    Printer.Canvas.TextOut(0, Line, Lst[I]);     {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns     a negative number. So Abs() is applied to the Height to make it a non-negative     value}    Line := Line + Abs(Printer.Canvas.Font.Height);    if (Line >= Printer.PageHeight) then      Printer.NewPage;  end;  Printer.EndDoc;end;
the procedure gives en empty page from the printer. What is wrong?

the code to get the file in Memo1:


--- 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";}};} ---[procedure TfrmPrinterSetUp.LoadMemoFromFile(FileName: string);var fileData: TStringlist;begin   Memo1.Text:= '';   fileData := TStringList.Create;        // Create the TSTringList object   fileData.LoadFromFile(FileName);        // Load from Testing.txt file   Memo1.Text := fileData.Text;   fileData.Free;end;   

 The file did load correctly ... What is going wrong?

jamie:
First off, use code tags so we can see it better  :D That's the number sign icon, click that and then paste your code into it.

As for the printing, it is a TstringList I assume, you are not indexing it.

Lst[your loop variable];


Also, make sure you are using the "printers4Lazarus" package in your project or something liek that.


Sprek Skik:

--- Quote from: jamie on May 19, 2024, 08:59:22 pm ---
As for the printing, it is a TstringList I assume, you are not indexing it.


--- End quote ---
What do you mean with indexing? I am just a beginner... sorry

jamie:
There was no INDEX being shown because you pasted it in the forums general text format, which will intercept [ x ] and things like it.
 
  Have you actually looked at the contents of lst StringList ?

  You should be using a TStringList , not Tstrings.

  In anycase, that that lst stringlist and assign it to a Tmemo to examine the contents.


EDIT:

 If you use your debugger, placing a break point at the start of the printing cycle, you can step the program using F7,F8 and hover the mouse over variables etc, to track the progress and see values.

dbannon:
I always set the printerfont  colour and size, they probably have sensible defaults but maybe not ? eg


--- 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";}};} ---    Printer.Canvas.Font.Size := 12;    Printer.Canvas.Font.Color := clBlack;
Anyway, as Jamie said, you need to check the content, 'Line' should increase with each loop (But MUST be reset when you start a new page by the way).  To make it easy to debug, you might like to add a line or two -


--- 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";}};} ---    procedure TfrmPrinterSetUp.PrintTStrings(Lst : TStrings) ;    var      I,      Line : Integer;      AString : string;                  // add this    begin      I := 0;      Line := 0 ;      Printer.BeginDoc ;      showmessage(IntToStr(Lst.Count-1));      for I := 0 to Lst.Count - 1 do begin        AString := List[i];                                        // and add this        Printer.Canvas.TextOut(0, Line, Lst[I]);    // set debugger to stop here             {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns         a negative number. So Abs() is applied to the Height to make it a non-negative         value}        Line := Line + Abs(Printer.Canvas.Font.Height);        if (Line >= Printer.PageHeight) then          Printer.NewPage;      end;      Printer.EndDoc;    end;
A bit easier for a beginner, set the debugger up to stop on the line AFTER "AString ;= " line, click on that line, then click in the 'left gutter' (the area to the left, where line number appear). That will set a red background under that line meaning the debugger will stop there.

A new project will have debugging turned on, so, now just click 'run', your app will startup, get as far as that line and stop, waiting for you to reclick the green 'run' triangle.  While its in that waiting mode, hover your mouse over variables of interest and you will see their current value. You can step through, line by line with eg F8

Davo

Navigation

[0] Message Index

[#] Next page

Go to full version