Link to home
Start Free TrialLog in
Avatar of Ioannis Anifantakis
Ioannis AnifantakisFlag for Greece

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Ioannis Anifantakis

ASKER

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?
>>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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial