Print any type of document using Delphi
2009-11-17 12:55
225 查看
Print any type of document using Delphi
1.Drop a TComboBox on a form. Name it "cboPrinter". Set Style to csDropDownList.
2.Put the next two lines in the form's OnCreate event handler.
// have available printers in the combo box
cboPrinter.Items.Assign(printer.Printers);
// pre-select the default / active printer
cboPrinter.ItemIndex := printer.PrinterIndex;Now, here's the method you can use to print any document type to a specified printer:
uses ShellAPI, Printers;
...
procedure TForm1.PrintDocument(const documentToPrint : string) ;
var
printCommand : string;
printerInfo : string;
Device, Driver, Port: array[0..255] of Char;
hDeviceMode: THandle;
begin
if Printer.PrinterIndex = cboPrinter.ItemIndex then
begin
printCommand := 'print';
printerInfo := '';
end
else
begin
printCommand := 'printto';
Printer.PrinterIndex := cboPrinter.ItemIndex;
Printer.GetPrinter(Device, Driver, Port, hDeviceMode) ;
printerInfo := Format('"%s" "%s" "%s"', [Device, Driver, Port]) ;
end;
ShellExecute(
Application.Handle,
PChar(printCommand),
PChar(documentToPrint),
PChar(printerInfo),
nil,
SW_HIDE
);
end;
Note
To use this code you'll need an application registered for each file type you want to print.
1.Drop a TComboBox on a form. Name it "cboPrinter". Set Style to csDropDownList.
2.Put the next two lines in the form's OnCreate event handler.
// have available printers in the combo box
cboPrinter.Items.Assign(printer.Printers);
// pre-select the default / active printer
cboPrinter.ItemIndex := printer.PrinterIndex;Now, here's the method you can use to print any document type to a specified printer:
uses ShellAPI, Printers;
...
procedure TForm1.PrintDocument(const documentToPrint : string) ;
var
printCommand : string;
printerInfo : string;
Device, Driver, Port: array[0..255] of Char;
hDeviceMode: THandle;
begin
if Printer.PrinterIndex = cboPrinter.ItemIndex then
begin
printCommand := 'print';
printerInfo := '';
end
else
begin
printCommand := 'printto';
Printer.PrinterIndex := cboPrinter.ItemIndex;
Printer.GetPrinter(Device, Driver, Port, hDeviceMode) ;
printerInfo := Format('"%s" "%s" "%s"', [Device, Driver, Port]) ;
end;
ShellExecute(
Application.Handle,
PChar(printCommand),
PChar(documentToPrint),
PChar(printerInfo),
nil,
SW_HIDE
);
end;
Note
To use this code you'll need an application registered for each file type you want to print.
相关文章推荐
- Using COM to Transfer Any Type of Data
- Matlab出现On Startup: Error using eval undefined function 'workspacefunc' for input arguments of type
- Delphi Question: Proper using of a Record Field/Property in a Class?
- Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案
- could not load any Objective-C class information. This will significantly reduce the quality of type
- CRT解决ssh远程登陆解决Unable to authenticate using any of the configured authenticatio
- 利用Delphi的“File Of Type”创建并管理属于你自己的“数据库”
- Top 20 Online Tools Useful For Any Type of Web Developer
- Cannot find any information on property [datetime] in a bean of type
- BizTalk Exception: This Assembler cannot retrieve a document specification using this type: "http://www.microsoft.com/biztalk/bank#BankQuoteRequest"
- delphi调用C#写的web服务例子-----------(重点 InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceSoap), ioDocument);)
- Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题
- javaweb开发常见错误Cannot find any information on property 'XXX' in a bean of type 'XXX'
- 【问题】Cannot find any information on property 'userPhone' in a bean of type 'Bean'.
- Write a method to print the last K lines of an input file using C++
- 论文读书笔记-THE PECULIARITIES OF THE TEXTDOCUMENTREPRESENTATION, USINGONTOLOGY AND TAGGING-BASED CLUSTERI
- gradle Could not create service of type CrossBuildFileHashCache using BuildSessionScopeServices.crea
- Cannot find any information on property 'User' in a bean of type 'com.entity.user'
- Creation of a Word 2007 document using the Open XML Format SDK
- Cannot find any information on property 'S' in a bean of type 'beans.Circle'