### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: <Solved>Easy printing TMemo in my case Memo1.Lines  (Read 725 times)

#### Sprek Skik

• New Member
• Posts: 21
• Beginner Lazarus & Free Pascal
##### <Solved>Easy printing TMemo in my case Memo1.Lines
« on: May 19, 2024, 08:06:39 pm »
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  [Select][+][-]
1. procedure TfrmPrinterSetUp.PrintTStrings(Lst : TStrings) ;
2. var
3.   I,
4.   Line : Integer;
5. begin
6.   I := 0;
7.   Line := 0 ;
8.   Printer.BeginDoc ;
9.   showmessage(IntToStr(Lst.Count-1));
10.   for I := 0 to Lst.Count - 1 do begin
11.     Printer.Canvas.TextOut(0, Line, Lst[I]);
12.
13.     {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
14.      a negative number. So Abs() is applied to the Height to make it a non-negative
15.      value}
16.     Line := Line + Abs(Printer.Canvas.Font.Height);
17.     if (Line >= Printer.PageHeight) then
18.       Printer.NewPage;
19.   end;
20.   Printer.EndDoc;
21. end;

the procedure gives en empty page from the printer. What is wrong?

the code to get the file in Memo1:

Code: Pascal  [Select][+][-]
2. var
3.  fileData: TStringlist;
4. begin
5.    Memo1.Text:= '';
6.    fileData := TStringList.Create;        // Create the TSTringList object
8.    Memo1.Text := fileData.Text;
9.    fileData.Free;
10. end;

The file did load correctly ... What is going wrong?
« Last Edit: May 20, 2024, 12:37:51 pm by Sprek Skik »

#### jamie

• Hero Member
• Posts: 6290
##### Re: Easy printing TMemo in my case Memo1.Lines
« Reply #1 on: May 19, 2024, 08:59:22 pm »
First off, use code tags so we can see it better  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.

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

The only true wisdom is knowing you know nothing

#### Sprek Skik

• New Member
• Posts: 21
• Beginner Lazarus & Free Pascal
##### Re: Easy printing TMemo in my case Memo1.Lines
« Reply #2 on: May 19, 2024, 10:11:15 pm »

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

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

#### jamie

• Hero Member
• Posts: 6290
##### Re: Easy printing TMemo in my case Memo1.Lines
« Reply #3 on: May 19, 2024, 10:35:32 pm »
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.

« Last Edit: May 19, 2024, 10:41:57 pm by jamie »
The only true wisdom is knowing you know nothing

#### dbannon

• Hero Member
• Posts: 2855
##### Re: Easy printing TMemo in my case Memo1.Lines
« Reply #4 on: May 20, 2024, 01:53:37 am »
I always set the printerfont  colour and size, they probably have sensible defaults but maybe not ? eg

Code: Pascal  [Select][+][-]
1.     Printer.Canvas.Font.Size := 12;
2.     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  [Select][+][-]
1.     procedure TfrmPrinterSetUp.PrintTStrings(Lst : TStrings) ;
2.     var
3.       I,
4.       Line : Integer;
5.       AString : string;                  // add this
6.     begin
7.       I := 0;
8.       Line := 0 ;
9.       Printer.BeginDoc ;
10.       showmessage(IntToStr(Lst.Count-1));
11.       for I := 0 to Lst.Count - 1 do begin
12.         AString := List[i];                                        // and add this
13.         Printer.Canvas.TextOut(0, Line, Lst[I]);    // set debugger to stop here
14.
15.         {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
16.          a negative number. So Abs() is applied to the Height to make it a non-negative
17.          value}
18.         Line := Line + Abs(Printer.Canvas.Font.Height);
19.         if (Line >= Printer.PageHeight) then
20.           Printer.NewPage;
21.       end;
22.       Printer.EndDoc;
23.     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
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

#### Sprek Skik

• New Member
• Posts: 21
• Beginner Lazarus & Free Pascal
##### Re: Easy printing TMemo in my case Memo1.Lines
« Reply #5 on: May 20, 2024, 11:44:35 am »
I always set the printerfont  colour and size, they probably have sensible defaults but maybe not ? eg

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 -

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
Thanks for the reply!  I made the changes with the setup of printer. It made the difference a small text file is ok, but with a larger file the program makes  a lot of empty pages.

I think you are right with resetting Line, but what code should I add to make this work? Thanks for the extra info about debugging it is all new for me.

#### Sprek Skik

• New Member
• Posts: 21
• Beginner Lazarus & Free Pascal
##### Re: Easy printing TMemo in my case Memo1.Lines
« Reply #6 on: May 20, 2024, 12:36:40 pm »
Well I have solved it!

The code;

Code: Pascal  [Select][+][-]
1. procedure TfrmPrinterSetUp.PrintMemo;
2. var
3.   I,
4.   Line : Integer;
5.
6. begin
7.   I := 0;
8.   Line := 0 ;
9.   Printer.BeginDoc ;
10.   Printer.Canvas.Font.Name := 'MS Sans Serif';
11.   Printer.Canvas.Font.Size := 10;
12.   Printer.Canvas.Font.Color := clBlack;
13.   for I := 0 to Memo1.Lines.Count - 1 do begin
14.     Printer.Canvas.TextOut(0, Line, Memo1.Lines[I]);
15.
16.     {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
17.      a negative number. So Abs() is applied to the Height to make it a non-negative
18.      value}
19.     Line := Line + Abs(Printer.Canvas.Font.Height);
20.     if (Line >= Printer.PageHeight) then
21.       begin
22.         Line := 0;
23.         Printer.NewPage;
24.       end;
25.
26.   end;
27.   Printer.EndDoc;
28. end;
29.