Solved

Line-by-Line Printing

Posted on 1997-08-05
12
293 Views
Last Modified: 2010-04-04
How do I print line-by-line in Delphi 2?
0
Comment
Question by:Evil
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 3

Expert Comment

by:mirek071497
ID: 1340559
Line by line - mean's not on canvas but as DOS doing this ?
0
 
LVL 1

Expert Comment

by:jturpin
ID: 1340560
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
 

Author Comment

by:Evil
ID: 1340561
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 3

Expert Comment

by:mirek071497
ID: 1340562
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
 

Author Comment

by:Evil
ID: 1340563
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
 
LVL 1

Expert Comment

by:millerw
ID: 1340564
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
 
LVL 1

Expert Comment

by:millerw
ID: 1340565
Sorry, that line should be:

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

Expert Comment

by:millerw
ID: 1340566
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
 

Author Comment

by:Evil
ID: 1340567
All that does is lock up the program
0
 
LVL 1

Expert Comment

by:millerw
ID: 1340568
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
 
LVL 1

Expert Comment

by:millerw
ID: 1340569
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
 
LVL 1

Accepted Solution

by:
millerw earned 100 total points
ID: 1340570
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question