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?
LVL 6
Ioannis AnifantakisProgramming InstructorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ziolkoCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ziolkoCommented:
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.
0
Ioannis AnifantakisProgramming InstructorAuthor 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?
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

ziolkoCommented:
>>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.
0
ziolkoCommented:
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.
0
JohnjcesCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.