How can I select the printer with printer?

Hello guys.

I have three printers installed in my computer, One of them is for a special printer for labels, I can leave this printers as default in my system only to set the drivers when I use the system for labels. In my cod I am using a this line
Printer.PrinterIndex := Printer.Printers.IndexOf('NAME_OF_MY_PRINTER'), but it is not working fine I think that something is missing, maybe to load another driver. Below you can see the code of my system. Maybe, there is a way to change default printer and after printing I can come back it.


 Printer.PrinterIndex := Printer.Printers.IndexOf('ETIQUETACLIENTE');
  With Printer do
  Begin
    Orientation := poLandscape;
    BeginDoc;
   // Canvas.StretchDraw(Rect(0,30,130,130),Image1.Picture.Graphic);
    Canvas.Pen.Width := 2;
    Canvas.Font.Name := 'Times New Roman';
    Canvas.Font.Size := 16;
    Canvas.TextOut(150,40, Cli);
    Canvas.MoveTo (150,90);
    Canvas.LineTo (600,90);
    Canvas.TextOut(150,110 ,'Romaneio :' + Rom);

    Canvas.Font.Size := 12;
    Canvas.TextOut(10,195,'Quantidade: ' + Qtd);
    Canvas.TextOut(10,255,'Código....: ' + Cod);
    Canvas.TextOut(10,315,'Descrição.: ' + Desc);
    Canvas.TextOut(10,375,'Locação...: ' + loc);

    EndDoc;
  End;
LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Pierre CorneliusCommented:
This demo will show you how to select a specific printer. Alternatively you could select a printer using the Printer.Getprinter function.

PAS
===
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Printers;

type
  TForm1 = class(TForm)
    PrinterSetupDialog1: TPrinterSetupDialog;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrinterSetupDialog1.Execute;
  ShowMessage('Printer selected: '+ Printer.Printers[Printer.PrinterIndex]);
end;

end.


DFM
===
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 197
  Height = 91
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 16
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object PrinterSetupDialog1: TPrinterSetupDialog
    Left = 112
    Top = 8
  end
end

Regards
Pierre
Scay7Commented:
For this example i place a printersetupdialog, button and a memo on a form.
For the onlick event for the button i have the following:


procedure TForm1.Button1Click(Sender: TObject);
var
oldprinter : integer;
begin
memo1.Lines.Add('Pre Printer was:');
memo1.Lines.Add(Printer.Printers[Printer.PrinterIndex]);
  oldprinter := Printer.PrinterIndex;
memo1.Lines.Add('');
memo1.Lines.Add('Printer Setup Started.');
  printersetupdialog1.Execute;
memo1.Lines.Add('New Selected Printer is:');
memo1.Lines.Add(Printer.Printers[Printer.PrinterIndex]);
memo1.Lines.Add('');

  // add your printing stuff here

  printer.PrinterIndex := oldprinter;
memo1.Lines.Add('Printer Set back to:');
memo1.Lines.Add(Printer.Printers[Printer.PrinterIndex]);
end;


This should get the DEFAULT printer save it, then with the PRINTERSETUPDIALOG select the new printer afterwards change the printer back to the default one...

Peace Scay7

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
saravananvgCommented:
Hello sir,

  You could check the following site for various operations you can perform on the printer.

http://www.swissdelphicenter.ch/torry/printing.php

with regards,
padmaja.
HanZuluCommented:
Changing the index for the printerindex works fine when you select
1 of the printers from a list.

try to select each one of the printers using their id and not indexof.
this code works fine with a 2 printer computer.

for default :

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to printer.printers.Count -1 do
    lstPrinters.items.Add (printer.printers[i]);
  printer.PrinterIndex := -1 ;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Print;
end;

for first printer

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to printer.printers.Count -1 do
    lstPrinters.items.Add (printer.printers[i]);
  printer.PrinterIndex := 1 ;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Print;
end;

for second printer

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to printer.printers.Count -1 do
    lstPrinters.items.Add (printer.printers[i]);
  printer.PrinterIndex := 0 ;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Print;
end;

Works fine.
Scay7Commented:
quote "Maybe, there is a way to change default printer and after printing I can come back it."

which is what the below code does...

Hanzulu:

procedure TForm1.Button1Click(Sender: TObject);
var oldprinter : integer;
begin
  oldprinter := Printer.PrinterIndex;  //<-- saves the default printer, which any PC has no matter how many printers there  
                                                    //      are. and i save that index...
  printersetupdialog1.Execute; //<-- you select the printer you want to print to changing from the default one.

  // add your printing stuff here, printing with the new set printer

  printer.PrinterIndex := oldprinter; //<-- set the default printer back, cause i saved its index...
end;

why make so many procedures for a simple question ?  :PP

Peace Scay7
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.