We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Line-by-Line Printing

Evil
Evil asked
on
Medium Priority
347 Views
Last Modified: 2010-04-04
How do I print line-by-line in Delphi 2?
Comment
Watch Question

Line by line - mean's not on canvas but as DOS doing this ?

Commented:
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 ?

Author

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.
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 ?

Author

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.

Commented:
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

Commented:
Sorry, that line should be:

WriteLn(F,'This will print immediately to the printer');
CloseFile(F);

Commented:
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

Author

Commented:
All that does is lock up the program

Commented:
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

Commented:
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.
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.