We help IT Professionals succeed at work.

Select a printer and print text or file in Delphi

I can get a list of available printers in delphi through the
Printer.Printers.Text

and I can also select a printer by its index like
myPrinterName:=Printer.Printers[i] (where i is the index of my desired printer).

How can I textout some string or file to the desired printer in delphi?
Comment
Watch Question

software developer
CERTIFIED EXPERT
Top Expert 2008
Commented:
set PrinterIndex to printer you want to use

then :

procedure TForm1.Button1Click(Sender: TObject);
var
  r: TRect;
  i: Integer;
begin
  with Printer do
    begin
      r := Rect(200,200,(Pagewidth - 200),(PageHeight - 200));
      BeginDoc;
       Canvas.TextOut(200,200, 'hello world');
      EndDoc;
    end;
end;

ziolko.
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
lsomething like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Printer do
    begin
      PrinterIndex := your_printer_index;
      BeginDoc;
       Canvas.TextOut(200,200, 'hello world');
      EndDoc;
    end;
end;

ziolko.
Ioannis AnifantakisProgramming Instructor

Author

Commented:
That seems to select different printers and draw the text on the canvas...

Just a hint...

if some of my printers are inkjet-laser and others are Dot-Matrix, then the textout is really bad when it gets to the dot-matrix one...

Is there any way arround this so I can print correctly on my desired printer either if its inkjet-laser or DotMatrix?
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
>>That seems to select different printers and draw the text on the canvas...

yup, that's how it works you can textout() or draw() onprinters canvas like on any other canvas

>>Is there any way arround this so I can print correctly on my desired printer either if its inkjet-laser or DotMatrix?

well it doesn't matter if it is laser, dot-matrix or termal printer it's all up to you what you draw on canvas and where you draw it.

for your "calculations" you can use:
  printer.Canvas.TextHeight('Lj')
  printer.Canvas.TextWidth('W')
as well as
 GetDeviceCaps(Printer.Handle, ... )


ziolko.
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
I used to use coordinates in milimeters to print text in specific position regardles of printer, it worked on different laser printers but never tried with dot-matrix.

basically i used:
  res := GetDeviceCaps(printer.Handle, PHYSICALWIDTH);
  sze := GetDeviceCaps(printer.Handle, HORZSIZE);
to calculate device units per millimeter (res / sze)
then
  offX := GetDeviceCaps(printer.Handle, PHYSICALOFFSETX)
to get edge of non-printable area, then when I wanted to print something 90 millimeters from lefte edge of page I calculated 90 millimeters to device units and substract offX

ziolko.
Commented:
Also, if you are worried about fonts and I didn't read this from others, you can specify the canvas font used to print any text you have.

i.e.

   Printer.Canvas.Font.Name := 'Times New Roman';
   Printer.Canvas.Font.Style := [fsBold];
   Printer.Canvas.Font.Size := 12;

Since you already know how to get the printer, below is a routine I use to directly print one record shown on a form. (I use a printer dialog since this is user initiated).

John
procedure PrintCurrentRecord1Click(Sender: TObject);
var
 POutput : TextFile;
 Image : TImage;
 PrX, PrY, Res : Integer;
begin
 if PrintDialog1.Execute then
  begin
   Screen.Cursor := crHourGlass;
   if not Expanded then
    FullyExpand1.Click;
   AssignPrn(POutput);
   Rewrite(POutput);
   // Prints the graph at the top of the form.
   Image := TImage.Create(self);
   if dbImageAsp1.Picture.Graphic <> nil then
    begin
     Image.Picture.Graphic := dbImageAsp1.Picture.Graphic;
     Res := Round(Printer.PageWidth div 8);
     PrX := Round(((Res * 2) / dbImageAsp1.Picture.Width) * dbImageAsp1.Picture.Width);
     PrY := Round(((Res * 2) / dbImageAsp1.Picture.Height) * dbImageAsp1.Picture.Height);
     Printer.Canvas.StretchDraw(Rect(3000, 200, PrX+3000, PrY+200), Image.Picture.Graphic); //100, 150
    end;
  try
   Printer.Canvas.Font.Name := 'Times New Roman';
   Printer.Canvas.Font.Style := [fsBold];
   Printer.Canvas.Font.Size := 12;
   WriteLn(POutput, ' ');
   Printer.Canvas.Font.Style := [fsUnderLine];
   WriteLn(POutput, '                                                                                                                     ');
   Printer.Canvas.Font.Style := [];
   WriteLn(POutput, ' ');
   WriteLn(POutput, '           ' + AlbumTitle.Text);
   WriteLn(POutput, '           ' + ArtistName.Text);
   WriteLn(POutput, '           ' + GenreEdit.Text);
   WriteLn(POutput, '           PUBLISHED: ' + YearEdit.Text);
   WriteLn(POutput, '           LIBRARY: ' + DacalLibNum.Text + ',  SLOT: ' + DacalPosNum.Text);
   WriteLn(POutput, ' ');
   WriteLn(POutput, ' ');
   Printer.Canvas.Font.Style := [fsUnderLine];
   WriteLn(POutput, '     Files/Tracks                                                                                            ');
   Printer.Canvas.Font.Style := [];
   WriteLn(POutput, ' ');
   DumpTreeviewToTextfile(POutput, TreeView1);
    finally
     CloseFile(POutput);
 end;
 Image.Free;
 Screen.Cursor := crDefault;
 end;
end;

Open in new window