Solved

Line-by-Line Printing

Posted on 1997-08-05
12
290 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now