• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 967
  • Last Modified:

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;
0
hidrau
Asked:
hidrau
5 Solutions
 
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
0
 
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
0
 
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.
0
 
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.
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now