Recent

Author Topic: Printing a free Pascal program  (Read 19897 times)

undermanager

  • Jr. Member
  • **
  • Posts: 58
Printing a free Pascal program
« on: February 10, 2010, 09:05:33 pm »
I'm learning Pascal in Lazarus. Could someone help me with printing, please. How do you send output to a printer? Do you have an example you could post that I could run and learn from?

I can't get this to work:

program Project2;

{$MODE Delphi}

                                  // Exercise 10.03

{$APPTYPE CONSOLE}                    // written by Sylvia Langfield

uses
  SysUtils, Printers, Graphics;

var PrintFile: TextFile;
  Message: String;

procedure SetPrintStyles
                   (Font: String; Size: Integer; Style: TFontStyles);
begin
  Printer.Canvas.Font.Name := Font;
  Printer.Canvas.Font.Size := Size;
  Printer.Canvas.Font.Style := Style;
end;

begin
  Write ('Enter a message to print: ');
  Readln (Message);
  AssignPrn (PrintFile);
  Rewrite (PrintFile);
  SetPrintStyles('Times New Roman',14,[fsBold, fsItalic]);
  Writeln (PrintFile, Message);
  SetPrintStyles('Arial',20,[fsBold, fsUnderline]);
  Writeln (PrintFile, Message);
  SetPrintStyles('Times New Roman',14,[]);
  Writeln (PrintFile, Message);
  CloseFile (PrintFile);
  Readln
end.

I get this when tryimng to run it:

Project2.lpr(26,13) Error: Identifier not found "AssignPrn"
Project2.lpr(27,21) Hint: Variable "PrintFile" does not seem to be initialized
Project2.lpr(37) Fatal: There were 1 errors compiling module, stopping


Any ideas, please?

davesimplewear

  • Sr. Member
  • ****
  • Posts: 319
    • Davids Freeware
Re: Printing a free Pascal program
« Reply #1 on: February 10, 2010, 09:12:18 pm »
Try this procedure

procedure PrintTStrings(Lst : TStrings) ;
var
  I,
  //Line : Integer;
begin
  I := 0;
  //Line := 0 ;
  Printer.BeginDoc ;
  for I := 0 to Lst.Count - 1 do begin
    Printer.Canvas.TextOut(0, Line, Lst);
    {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.TextHeight('I'));
    if (Line >= Printer.PageHeight) then
      Printer.NewPage;
  end;
  Printer.EndDoc;
end;

regards
Dave
All things considered insanity seems the best option

undermanager

  • Jr. Member
  • **
  • Posts: 58
Re: Printing a free Pascal program
« Reply #2 on: February 10, 2010, 09:40:16 pm »
Thanks. This is great. Have you got a full example, please? I get this error: Project2.lpr(16,39) Error: Identifier not found "TStrings"

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Printing a free Pascal program
« Reply #3 on: February 10, 2010, 10:03:57 pm »
You'll need to add the unit 'Classes' to the uses clause.

calebs

  • Full Member
  • ***
  • Posts: 190
Re: Printing a free Pascal program
« Reply #4 on: February 10, 2010, 10:27:43 pm »
Hi Talking about printing in lazarus, i want to print into a matrix printer, i don't want to use the canvas because i know it's graphic and takes ages to print something.
I'm doing a program that prints tickets and it needs speed.
Do you know any way to print direct to the printer (lpt1 in dos/win or using lpr or other ways in linux?)
Thanks

undermanager

  • Jr. Member
  • **
  • Posts: 58
Re: Printing a free Pascal program
« Reply #5 on: February 10, 2010, 10:29:24 pm »
Could someone post a complete example of printing a freePascal program for me to study, please.

davesimplewear

  • Sr. Member
  • ****
  • Posts: 319
    • Davids Freeware
Re: Printing a free Pascal program
« Reply #6 on: February 11, 2010, 05:17:00 am »
Calebs have a look at the raw print example in the components/printer folder or have a look at these delphi components
All things considered insanity seems the best option

undermanager

  • Jr. Member
  • **
  • Posts: 58
Re: Printing a free Pascal program
« Reply #7 on: February 11, 2010, 09:45:28 pm »
Is there a way to print from the free pascal console to the printer, anyone? Do you have an example of a simple program you could post, so I could see how it's done from Masters. Thank you. Signed.

Grasshopper.

 :D

 

TinyPortal © 2005-2018