[RESOLVED] Error printing the contents of a ListBox under Mac lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- pasquale · 2012-03-23 21:45 Hello, I have a problem printing out the contents of a ListBox on my Mac. The code I am using runs as follows: Code: [Select]procedure TfrmSearch.btnPrintClick(Sender: TObject);   var   MiaY   : Integer;   Conta  : Integer; begin   If lstRecords.Items.Count = 0 Then     begin       ShowMessage('Cannot print an empty list!');       Exit;     end;   if PrintDialog.Execute then     begin     Printer.BeginDoc;     Screen.Cursor := crHourGlass;     Printer.Canvas.Font.Color := clBlack;     Printer.Canvas.Font.Name := 'Courier New';     Printer.Canvas.Font.Size := 14;     Printer.Canvas.Font.Style := Printer.Canvas.Font.Style - [fsBold];     Printer.Canvas.Font.Style := Printer.Canvas.Font.Style - [fsUnderline];     MiaY := 0;     MiaY := MiaY + Abs(2* Printer.Canvas.Font.Height);     Printer.Canvas.TextOut(150,MiaY, '------------------------' );     Printer.Canvas.Font.Size := 14;     MiaY := MiaY + Abs(Printer.Canvas.Font.Height);     Printer.Canvas.TextOut(150,MiaY, 'Search Results');     MiaY := MiaY + Abs(Printer.Canvas.Font.Height);     Printer.Canvas.Font.Size := 14;     Printer.Canvas.TextOut(150,MiaY, '------------------------' );     Printer.Canvas.Font.Size := 8;     MiaY := MiaY + Abs(3* Printer.Canvas.Font.Height);     For Conta := 0 to lstRecords.Items.Count - 1  Do       begin       If MiaY >= Printer.PageHeight - Abs(3 * Printer.Canvas.Font.Height) Then       Begin         MiaY :=0;         MiaY := MiaY + Abs(2* Printer.Canvas.Font.Height);         Printer.NewPage;       End;         Printer.Canvas.TextOut(150, MiaY, lstRecords.Items[Conta]);         MiaY := MiaY + Abs(2* Printer.Canvas.Font.Height);       end;     Printer.Canvas.TextOut(150, MiaY + 50, '____________________' );     Printer.Canvas.TextOut(150, MiaY + 150, 'This report was printed on ' + DateToStr(Date) + ' at ' + Timetostr(time));     Printer.EndDoc;     Screen.Cursor := crDefault;   end; end; The code above seems to work fine under MS Windows where I get what you can see in the attached file. On the contrary, on my Mac multiple pages are printed, each one containing a single line. Are there any amendments to be made on Mac? TIA P.S. The personal data in the attached file are not real. ---------------------------------------------------------------------- jwdietrich · 2012-03-23 23:24 I cannot reproduce your problem. Here (Mac OS X 10.5, PowerMac G5 Dual, Lazarus 0.9.30.4, FPC 2.6) I get a result that might well be expected, see the attached screen shots from the generated application and from the preview of the print output. Could it bee that you have additional nonprintable characters in the items of your ListBox? You should keep in mind that Windows uses a sequence of CR and LF for encoding line endings, while the Mac uses either CR or LF, but not both (and Linux and BSD use LF only). ---------------------------------------------------------------------- pasquale · 2012-03-24 06:30 Hi, I used neither line endings nor any other special characters to populate my ListBox. I forgot to tell you that I had used the "PDF virtual printer" with my Mac, as I don't have any hardware printers connected to it at the moment. Could you please try exporting it to PDF instead of printing it on paper and tell me what result you get? Do you think the malfunctioning could depend on the fact that I have not set any unit of measures? I tried using Code: [Select]SetMapMode(printer.Canvas.Handle, MM_LOMETRIC); but this code seems not to work in Lazarus. I have also noticed that the space between the last item on the list and the bottom lines is much larger on your printout than on mine. Thanks a lot for your help. ---------------------------------------------------------------------- ludob · 2012-03-24 07:49 QuoteI had used the "PDF virtual printer" with my MacCheck the values for Printer.Canvas.Font.Height and Printer.PageHeight. PDF printers don't create new pages when you print outside the page or send control characters. So the new pages are created with Printer.NewPage. ---------------------------------------------------------------------- pasquale · 2012-03-24 08:06 Quote from: ludob on March 24, 2012, 08:49:27 amQuoteI had used the "PDF virtual printer" with my MacCheck the values for Printer.Canvas.Font.Height and Printer.PageHeight. PDF printers don't create new pages when you print outside the page or send control characters. So the new pages are created with Printer.NewPage. Hi. I checked the value of Printer.PageHeight: Code: [Select]ShowMessage(FloattoStr(Printer.PageHeight)); and it returned zero. What does it mean? ---------------------------------------------------------------------- ludob · 2012-03-24 08:30 Check Printer.PaperSize.DefaultPaperName if it returns the name of the printers default paper size. If it doesn't return anything try to select your paper size with Printer.PaperSize.Papername:='A4' or whatever size you use. Check also in the driver if you have set up the paper size correctly. ---------------------------------------------------------------------- pasquale · 2012-03-24 08:41 Hi. The code: Code: [Select]ShowMessage(Printer.PaperSize.DefaultPaperName); returns "A4". So, apparently, the paper size is set up correctly. Why that large space between the lines, then? How do I check the paper size in the driver? ---------------------------------------------------------------------- ludob · 2012-03-24 08:47 Could you also check the Printer.PaperSize.PaperRect.PhysicalRect and Printer.PaperSize.PaperRect.WorkRect top and bottom values? Check also the margins in the driver. ---------------------------------------------------------------------- pasquale · 2012-03-24 08:58 Hi. Printer.PaperSize.PaperRect.WorkRect.Top returned 18 while Printer.PaperSize.PaperRect.WorkRect.Bottom returned 801. Printer.PaperSize.PaperRect.PhysicalRect.Top returned 0 and Printer.PaperSize.PaperRect.PhysicalRect.Bottom returned 842. Are these values OK? Thanks. P.S. Just in case it could help, please find attached the PDF file I get when I print out the content of the ListBox. ---------------------------------------------------------------------- ludob · 2012-03-24 09:37 That means your printer is at 70 dpi. The values are normal. It think something went wrong when getting Printer.PageHeight because that is defined as WorkRect.bottom - WorkRect.top. Instead of retrieving n times Printer.Canvas.Font.Height and Printer.PageHeight in the for loop it would be better to get them ones, store them in a variable and use that variable in the loop. This shouldn't change the end result but it is more efficient. If Printer.PageHeight really returns 0 test against WorkRect.bottom - WorkRect.top. ---------------------------------------------------------------------- pasquale · 2012-03-24 09:57 Hi. While ShowMessage(FloattoStr(Printer.PageHeight)); continues to return 0, ShowMessage(FloattoStr(Printer.PaperSize.PaperRect.WorkRect.bottom - Printer.PaperSize.PaperRect.WorkRect.Top)); returns 783. Should the two values coincide? ---------------------------------------------------------------------- ludob · 2012-03-24 10:08 QuoteShould the two values coincide?Yes. GetPageHeight is implemented as Code: [Select]function TPrinter.GetPageHeight: Integer; begin   Result:=0;   if (Printers.Count>0) then     with PaperSize.PaperRect.WorkRect do       Result:=Bottom-Top; end;If you get a select printer dialog then Printers.Count>0. Note that I'm working with lazarus 0.9.31. ---------------------------------------------------------------------- pasquale · 2012-03-24 10:21 Please consider that I cannot select any printer from the dialog box because I still have to connect one to my Mac. What I do is just click on the "Create PDF" button that you get in the default printing dialog box. ---------------------------------------------------------------------- ludob · 2012-03-24 10:35 Quote from: pasquale on March 24, 2012, 11:21:10 amPlease consider that I cannot select any printer from the dialog box because I still have to connect one to my Mac. What I do is just click on the "Create PDF" button that you get in the default printing dialog box. Ah, that explains a lot. So printers.count is zero. So to solve your problem, create a variable pageheight:integer and assign Printer.PaperSize.PaperRect.WorkRect.bottom - Printer.PaperSize.PaperRect.WorkRect.Top to it. Use this variable instead of Printer.PageHeight in the test Code: [Select]If MiaY >= pageheight - Abs(3 * Printer.Canvas.Font.Height) ThenAnother solution would be to install a printer, even if its not connected. ---------------------------------------------------------------------- jwdietrich · 2012-03-24 10:58 For the case that the above mentioned hints don't help I have created a mini-application from scratch that does nothing but showing a list box and printing it. t works without any problem on Mac OS X Leopard. See the compressed source code archive in the attachment. Perhaps you can learn from the differences to your project what went wrong. ---------------------------------------------------------------------- pasquale · 2012-03-24 12:15 Hello. Setting  MiaY >= pageheight - Abs(3 * Printer.Canvas.Font.Height) worked like a charm! THANK YOU VERY MUCH INDEED, ludob and jwdietrich, for having solved my problem.