Line-by-Line Printing

How do I print line-by-line in Delphi 2?
EvilAsked:
Who is Participating?
 
millerwConnect With a Mentor Commented:
Here is some code that works.  It is like the old pascal way of Writeln (Lpt, 'Text');.  Look at the code and try it to see if it does what you want.  It should be exactly what you are looking for.  I used it and it works with Windows--it printed to my default printer in Windows.  Even if that printer is a network printer it will work.  Because the printer is a laser, I'm not sure if output is immediate line-by-line or canvas cached.  Check it on your printer and see, if it is cached, I'll see if I can find out how to get the old AssignFile to work in the above example code.  This one is probably the better bet.  There is the assembly way of doing is also if you want to check into that or if you want example code of how to do it.  I would make that my last priority though.  

Uses Printers;
procedure TForm1.FormCreate(Sender: TObject);
var
   F: TextFile;
begin
     AssignPrn(F);
     ReWrite (f);
     Writeln (F, 'Testing 1 2 3'+#12);
end;


Good Luck,
Scott
0
 
mirek071497Commented:
Line by line - mean's not on canvas but as DOS doing this ?
0
 
jturpinCommented:
A good way to print line by line, is to use the TPrinter object
in the unit Printers. (Look in Delphi help under TPrinter).

Some helpful hints:

   LineSpacePixelsY:=
      Round( Printer.Canvas.TextHeight('X') * SpaceBetweenLines );
      {Where SpaceBewteenLines = line spacing factor you want}

   AmountPrinted:= 0;

   LineHeight:= Printer.Canvas.TextHeight('X') + LineSpacePixelsY;

   MaxPrintLines:= (Printer.PageHeight div fLineHeight) - EndMargin;
      {Where EndMargin is the number of lines you want for the End Margin}

   CharsPerLine:= (Printer.PageWidth div Printer.Canvas.TextWidth('X'));


   Printer.Canvas.font.pitch:= fpFixed;


   
   {Then to print a line:}
   Printer.Canvas.TextOut(0,fAmountPrinted,LineAsString);
   fAmountPrinted:= fAmountPrinted + fLineHeight;

ok ?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
EvilAuthor Commented:
I mean line-by-line like DOS does.  It will print out that one line and not wait for the page to be full.
0
 
mirek071497Commented:
I can't test this because my printer work's only with windows (LEXMARK 1020) but i think so You must send character's directly to port. This is not good idea. Mayby You don't need this .Tell me what effect you need ?
0
 
EvilAuthor Commented:
I'm trying to print alarms so I need them to show up immediately on the printer instead of wait for the page to be full.
0
 
millerwCommented:
You need to send the information directly to LPT port.  It is the same as you would in DOS.  Open the port for output with Assign, ReWrite---then write out your information with Write/WriteLn.

If you have done it in DOS (Turbo Pascal) it is just the same.

Here is an example:

var
  F: TextFile;
begin
    AssignFile(F, 'LPT1');  
    ReWrite(F);
    Readln(F, 'This will print immediately on the printer');                                  CloseFile(F);
end;

Hope that satisfies you question and good luck!
Scott
0
 
millerwCommented:
Sorry, that line should be:

WriteLn(F,'This will print immediately to the printer');
CloseFile(F);
0
 
millerwCommented:
I was thinking that you might be doing this on a network and of course that will not work for a network.  That will only work for a PHYSICALLY connected printer to LPT1.  Here is the code for network printing:

var
   f: textfile;
begin
     Assignfile (f, '\\Pw_axp\1mbe1');
     Rewrite (f);
     WRiteln (f, 'This is a test'+#12);
     closefile(f);
end;

#12 is a page break----laser printers need this in order to finish a page and print it, otherwise you tie up the printer
0
 
EvilAuthor Commented:
All that does is lock up the program
0
 
millerwCommented:
That code works---I tried it right here.  Test the IOResult system variable.  Change the code to read the following:

var
   F: TextFile;
begin
{I-}
   AssignFile(F, 'LPT1');
   ReWrite(F);
   If IOResult = 0 then
   begin
        Writeln(F, 'This will print immediately on the printer');
        If IOResult <> 0 then
           MessageDlg ('Error Encountered: '+IntToStr(IOResult),
                      mtError, [mbOK], 0);
   end;
   else
       MessageDlg ('Error Encountered: '+IntToStr(IOResult),
                  mtError, [mbOK], 0);
   CloseFile(F);
{I+}

It doesn't lock up the computer.  I had it return an error after quite a while here at work due to the fact that nothing is on LPT1.  The Error was something to the effect of I/O error.  It does take a while for I/O errors to return in Windows---just wait and see what it will do.  I took about 30 sec to a minute for the error to return here at work.  You can use the {I-} to turn off I/O checking and the program will not "appear" to lock up.  Make sure you are using the correct port--Lpt1, Lpt2, etc.  Additionally, the code will tell you the error that was returned.  Then you can use help on IOResult to find out what the error returned means.

I will try the code on a system that actually has a printer on LPT1 tonight and make sure it works.  I'm positive that the code is valid---it definitely worked for the network printer.  Try the code above and see if you can get an error out of it---wait for it to return.  Patience is after all a virture.

Good Luck,
Scott
0
 
millerwCommented:
By the way, the error I got was I/O Error 21.  Couldn't find it in the help file, but it should mean the port has nothing connected to it.

Run the code above with F7 and trace it.  The line that should be having trouble is the WriteLn one.  It should take a long time to run and then do nothing (Change all of the {I-|+} to {$I-|+})

Sorry about the coding errors, I have not used these setting for a really long tine.  I used to do printer graphics all the time and that is where my knowledge is from.  Additionally, all of my code is not here but at home----if you want to see a program that will print out graphics, I can send you a copy of it from home.  I'm kinda rusty since it has been about 3-4 years since my last printer graphics program.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.