Board index » delphi » Printer problems

Printer problems

What is the best way to use the printer?
Is it the printer.canvas?
I have problem to have the fontstyle and size that I want.
I think I don't really understand how the printer.canvas works.
Can anyone give me some hint?
--
/*------------------------------------------*\
| Stefan Hagstedt <s...@lkab.se>              |
|                                            |
| Malmberget (100 km north the Artic circle) |
| Sweden                                     |
\*------------------------------------------*/

 

Re:Printer problems


Quote
Stefan Hagstedt wrote:

> What is the best way to use the printer?
> Is it the printer.canvas?
> I have problem to have the fontstyle and size that I want.
> I think I don't really understand how the printer.canvas works.
> Can anyone give me some hint?
> --
> /*------------------------------------------*\
> | Stefan Hagstedt <s...@lkab.se>              |
> |                                            |
> | Malmberget (100 km north the Artic circle) |
> | Sweden                                     |
> \*------------------------------------------*/

I've tried using some of the report writers and have been quite
dissatisfied with the time it takes them to load, etc (especially if I'm
printing a form of just one record, and I happen to be looking at the
record on screen!)

Anyway, I've experienced some problems with selection of fonts and sizes,
but, by an large have been happier with using the printer.canvas.font and
printer.canvas.textout, etc, rather than report writers.

What I do is first select the printer (using printer.printers[x], where x
represents the index number of the available printers.  You'll need to
look at the printer setup dialog box and the font dialog box that comes
with the program.

Then I set a variable 'n' equal to the number of pixels per inch on the
printer canvas. Since I've been having trouble with the PixelsPerInch
property, I use the following:  n:=Printer.PageWidth/8;  This is only for
8.5 wide paper (gives 1/4 inch margins left & right). All of the printers
I deal with have the same number of pixels horizontally as vertically per
inch, so I can use the same variable for positioning both.

I use 'x' to represent horizontal spacing and 'y' for vertical.
Then I use a variable 'c' for the text that I want to output.  Each line
of print on the page goes something like this:

        x:=round(n*2.5);                {2.5 inches from left}
        y:=round(n*1.25);               {1.25 inches from top}
        c:='Hello, world!';             {original, huh?}
        Printer.BeginDoc;
        Printer.Canvas.TextOut(x,y,c);
        Printer.EndDoc;

Of course, you'll probably want to change the variables many times and
use the textout method for each piece of printing.  And if so, only call
BeginDoc and EndDoc once (at the beginning and end).

If you have any more specific questions about your situation, you can
email me at ct...@earthlink.net.

Bob Jones
Custom Technologies
Then I select the font size.

Re:Printer problems


Quote
Stefan Hagstedt wrote:

> What is the best way to use the printer?
> Is it the printer.canvas?
> I have problem to have the fontstyle and size that I want.
> I think I don't really understand how the printer.canvas works.
> Can anyone give me some hint?
> --
> /*------------------------------------------*\
> | Stefan Hagstedt <s...@lkab.se>              |
> |                                            |
> | Malmberget (100 km north the Artic circle) |
> | Sweden                                     |
> \*------------------------------------------*/

When changing printers, be aware that fontsizes may
not always scale properly. To ensure proper scaling
set the PixelsPerInch property of the font.

Here are two examples:

********************************************************
uses Printers

var
  MyFile: TextFile;
begin
  AssignPrn(MyFile);
  Rewrite(MyFile);

  Printer.Canvas.Font.Name := 'Courier New';
  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.Font.PixelsPerInch:=
    GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY);

  Writeln(MyFile, 'Print this text');

  System.CloseFile(MyFile);
end;

******************************************************

uses Printers;

begin
  Printer.BeginDoc;
  Printer.Canvas.Font.Name := 'Courier New';
  Printer.Canvas.Font.Style := [fsBold];

  Printer.Canvas.Font.PixelsPerInch:=
    GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY);

  Printer.Canvas.Textout(10, 10, 'Print this text');

  Printer.EndDoc;
end;

Hope this helps!

Joe

--
Joe C. Hecht
Borland Delphi Developer Support Group
http://www.borland.com

Other Threads