Thread View: comp.lang.pascal.delphi.misc
3 messages
3 total messages
Started by venkateswarag
Sun, 11 Sep 2005 04:07
Printing PCL Format direct from Delphi
Author: venkateswarag
Date: Sun, 11 Sep 2005 04:07
Date: Sun, 11 Sep 2005 04:07
13 lines
1621 bytes
1621 bytes
Hi, Can anyone help me in the following !!! How to get the list of installed printers and display the list in the dropdown box using Tprinter class. I need to send the PCL format file to printer directly for selected printer from the dropdown list. Thanks in advance Thanks sai
Re: Printing PCL Format direct from Delphi
Author: J French
Date: Sun, 11 Sep 2005 07:19
Date: Sun, 11 Sep 2005 07:19
22 lines
1904 bytes
1904 bytes
On 10 Sep 2005 21:07:52 -0700, venkateswarag@gmail.com wrote: >Hi, > >Can anyone help me in the following !!! >How to get the list of installed printers and display the list in the >dropdown box using Tprinter class. ListBox1.Items.Text := Printer.Printers.Text; >I need to send the PCL format file to printer directly for selected >printer from the dropdown list. You therefore need to bypass the printer's canvas The simplest method I know of is to use the OpenPrinter API then StartDocPrinter then possibly StartPagePrinter then dump the contents of the file using: WritePrinter then possibly EndPagePrinter then EndDocPrinter then ClosePrinter
Re: Printing PCL Format direct from Delphi
Author: John P
Date: Tue, 13 Sep 2005 08:39
Date: Tue, 13 Sep 2005 08:39
80 lines
3672 bytes
3672 bytes
venkateswarag@gmail.com wrote:
>
> I need to send the PCL format file to printer directly for selected
> printer from the dropdown list.
If you could write that dropdown list your self. And if you can deal
with the the thing that the printers here are indentified by their
PrinterIndex number, then this snippet should do the job.
uses
.. Printers, WinSpool;
procedure DumpAnyFileToPrinter(aFileName: string; aPrinterIndex:
Integer);
var
BytesWritten: DWORD;
aHandle: THandle;
DocInfo: TDocInfo1;
PrinterName: PChar;
MSt: TMemoryStream;
aErrCode: Integer;
aFailed: Boolean;
begin
if not FileExists(aFileName) then
raise exception.create(Format('File not found "%S"', [aFileName]));
try
PrinterName := PChar(Printer.Printers[aPrinterIndex]);
except
ShowMessage(Format('Index [%d] outside current printer count (%d).',
[aPrinterIndex, Printer.Printers.Count]));
Exit;
end;
if not WinSpool.OpenPrinter(PrinterName, aHandle, nil) then
raise exception.create('Printer not found');
MSt := TMemoryStream.Create;
try
MSt.LoadFromFile(aFileName);
{Name pDocName will be shown on the Print Task list.}
DocInfo.pDocName := PChar('DUMP: ' + aFileName);
DocInfo.pOutputFile := nil;
DocInfo.pDatatype := 'RAW';
if StartDocPrinter(aHandle, 1, @DocInfo) <> 0 then
try
if StartPagePrinter(aHandle) then
WritePrinter(aHandle, MSt.Memory, MSt.Size, BytesWritten);
finally
EndPagePrinter(aHandle);
end;
EndDocPrinter(aHandle);
finally
aErrCode := GetLastError;
WinSpool.ClosePrinter(aHandle);
if aErrCode = 0 // 0 = Last operation Succeed.
then MessageBox(0, PChar(Format('Success! %d bytes spooled',
[BytesWritten])), PrinterName, mb_OK + mb_iconinformation)
else MessageBox(0, PChar(SysErrorMessage(aErrCode)),
'Printing Error', mb_OK + mb_iconexclamation);
MSt.Free;
ClosePrinter(aHandle);
end;
end;
procedure TForm1.btnRaw2Click(Sender: TObject);
begin
// TEST.PCL is a file created by printing through a LaserJet
// PCL-driver, with 'Print To File' option.
// Dump this file to the current active printer.
DumpAnyFileToPrinter('C:\TEST.PCL', Printer.PrinterIndex);
end;
There may be even too much built in checkings, and too many hard coded
error messages for this kind of task.
And the code is not perfect either, but it has served me through the
years, for my occasional Dump needs.
John P
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads