Recent

Author Topic: (SOLVED) Printing in Lazarus  (Read 11628 times)

odvesims

  • Full Member
  • ***
  • Posts: 176
(SOLVED) Printing in Lazarus
« on: November 09, 2016, 04:18:43 pm »
Hello!

I'm using Printers unit to print from my application. I'm using Raw printing mode to print directly to the printer. The printing goes directly to the printer set as default in my Control Panel. Now, here's the question.. If I have multiple printers installed in my computer and all are connected, I'd like to select the one I want to send the printing to. I've tried using the printer.SetPrinter(string containing printer name) but it's still printing to the default printer.

Is there any way to change which printer to send the printing to..?
« Last Edit: November 09, 2016, 08:29:13 pm by odvesims »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Printing in Lazarus
« Reply #1 on: November 09, 2016, 04:39:53 pm »
The printer name has to match exactly. This can be tiresome.

Try using the printers property to get a list of names of the printers. Alternatively try using printerindex property. Note that i have not tested this, but in theory should work.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printing in Lazarus
« Reply #2 on: November 09, 2016, 07:03:15 pm »
The printer name has to match exactly. This can be tiresome.

Try using the printers property to get a list of names of the printers. Alternatively try using printerindex property. Note that i have not tested this, but in theory should work.

Is there any way I can find an example on this?

MichaelBM

  • New Member
  • *
  • Posts: 38
Re: Printing in Lazarus
« Reply #3 on: November 09, 2016, 07:39:19 pm »
Have you seen this?

http://wiki.freepascal.org/Using_the_printer

Example:

Code: Pascal  [Select][+][-]
  1. uses Printers;
  2.  
  3. function GetDefaultPrinterName : string;
  4. begin
  5.   if (Printer.PrinterIndex > 0) then begin
  6.     Result := Printer.Printers[Printer.PrinterIndex];
  7.   end else begin
  8.     Result := '';
  9.   end;
  10. end;
  11.  
« Last Edit: November 09, 2016, 07:44:27 pm by MichaelBM »
"Everything must be made as simple as possible. But not simpler.”

sfeinst

  • Sr. Member
  • ****
  • Posts: 259
Re: Printing in Lazarus
« Reply #4 on: November 09, 2016, 07:45:18 pm »
Are you purposely not using the printer dialog (maybe GUI-less app or something)?  If not, I use the dialog and the Printer object always uses the printer selected using the dialog.

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printing in Lazarus
« Reply #5 on: November 09, 2016, 08:02:46 pm »
Have you seen this?

http://wiki.freepascal.org/Using_the_printer

Example:

Code: Pascal  [Select][+][-]
  1. uses Printers;
  2.  
  3. function GetDefaultPrinterName : string;
  4. begin
  5.   if (Printer.PrinterIndex > 0) then begin
  6.     Result := Printer.Printers[Printer.PrinterIndex];
  7.   end else begin
  8.     Result := '';
  9.   end;
  10. end;
  11.  

Thanks Michael. I was able to check the example they pointed at for changing printer and was able to do it with this line:

Code: Pascal  [Select][+][-]
  1. Printers.Printer.SetPrinter(printer_name);
Are you purposely not using the printer dialog (maybe GUI-less app or something)?  If not, I use the dialog and the Printer object always uses the printer selected using the dialog.

I have the printers listed in a ComboBox and want to set the printer whenever the user changes the selected item in the ComboBox.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Printing in Lazarus
« Reply #6 on: November 09, 2016, 08:04:57 pm »
No need for difficult overused dialogs  :D

example attached, looks like:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4. begin
  5.   ListBox1.Items.Clear;
  6.  
  7.   For i := 0 to Pred(Printer.Printers.Count) do
  8.   begin
  9.     ListBox1.Items.Add(Printer.Printers[i]);
  10.   end;
  11. end;
  12.  
  13.  
  14. procedure TForm1.ListBox1Click(Sender: TObject);
  15. begin
  16.   If ListBox1.ItemIndex <> -1 then
  17.   begin
  18.     Printer.PrinterIndex:= ListBox1.ItemIndex;
  19.     // Alternative method using SetPrinter():
  20.     // Printer.SetPrinter(ListBox1.Items[ListBox1.ItemIndex]);
  21.  
  22.     Memo1.Append('Current printer index switched to ' + IntToStr(Printer.PrinterIndex));
  23.     // From now on you should be able to print something to your chosen printer
  24.   end;
  25. end;
  26.  

Please let know if it still keeps defaulting to -1 (default printer). You should be able to click in the listbox and print something after the printerindex was changed.
« Last Edit: November 09, 2016, 08:06:37 pm by molly »

rvk

  • Hero Member
  • *****
  • Posts: 6991
Re: Printing in Lazarus
« Reply #7 on: November 09, 2016, 08:11:05 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4. begin
  5.   ListBox1.Items.Clear;
  6.  
  7.   For i := 0 to Pred(Printer.Printers.Count) do
  8.   begin
  9.     ListBox1.Items.Add(Printer.Printers[i]);
  10.   end;
  11. end;
No need to loop through the printers. Printer.Printers is a TStringList and so is Listbox1.Items so this works just fine :)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Listbox1.Items.Assign(Printer.Printers);
  4. end;

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Printing in Lazarus
« Reply #8 on: November 09, 2016, 08:18:50 pm »
Thank you rvk.

It is always a pleasure to get corrected when things can be done in a better way  :)

Please note that i did so on purpose to let TS better understand that it is indeed a list and that property PrinterIndex, property Printers and method SetPrinter are related to each other with this index, as i had the notion this was unclear for TS.

Other then that you are absolutely right  8-)

odvesims

  • Full Member
  • ***
  • Posts: 176
Re: Printing in Lazarus
« Reply #9 on: November 09, 2016, 08:28:37 pm »
Thanks everyone for your input..! Much appreciated ^_^

 

TinyPortal © 2005-2018