Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX, TXT
来源:互联网 发布:淘宝红包口令 编辑:程序博客网 时间:2024/05/22 06:09
If your Delphi application needs to operate on various types of files, one of the tasks you might have for your application is to allow the user of the application to print a file, whatever the file type is.
Most document oriented applications, like MS Word, MS Excel or Adobe "know" how to print documents they are "in charge of". For example, Word saves the text you write in documents with DOC extension. Since Word (Microsoft) determines what is the "raw" contents of a .DOC file it knows how to print .DOC files. The same applies for any "known" file type holding some printable information.
What if you need to print various types of documents / files from your application? Can you know how to send the file to the printer in order for it to be printed correctly? I guess the answer is no. At least I do not know :)
Print Any Type of Document (PDF, DOC, XLS, HTML, RTF, DOCX) Using Delphi
So, how do you print any type of document, programmatically using Delphi code?
Well I guess we should "ask" Windows: what application knows how to print, for example, a PDF file. Or even better we should tell to Windows: here's one PDF file, send it to the application associated / in charge of printing PDF files.
Open up Windows Explorer, navigate to a directory containing some printable files. For most of the file types on your system, when you right click a file in Windows Explorer, you will locate the "Print" command. Executing the Print shell command, will result in the file being sent to the default printer.
Well, that's exactly what we want - for a file type, call a method that will send the file to the associated application for printing.
The function we are after is the ShellExecute API function.
ShellExecute: Print / PrintTo
At its simplest, ShellExecute lets you programmatically start any application / open any file which is installed on the user's machine.
However, ShellExecute can do much more. ShellExecute can be used to launch application, open Windows Explorer, initiate a search beginning in the specified directory - and what's of most importance for us right now: prints the specified file.
Specify Printer for ShellExecute / Print
Here's how to print a file using the ShellExecute function:
ShellExecute(Handle, 'print', PChar('c:/document.doc'), nil, nil, SW_HIDE) ;
Note the second parameter : "print".
Using the above call, a document "document.doc" located on the root of the C drive will be sent to the Windows default printer.
ShellExecute always uses the default printer for the "print" action.
What if you need to print to a different printer, what if you want to allow the user to change the printer?
The PrintTo Shell Command
Some applications support the 'printto' action. PrintTo can be used to specify the name of the printer used for the print action. Printer is determined by 3 parameter: printer name, drive name and port.
Programmatically Printing Files
Ok, enough theory. Time for some real code:
Before you copy and paste: the Printer global variable (TPrinter type) available in all Delphi programs can be used to manage any printing performed by an application. Printer is defined in the "printers" unit, ShellExecute is defined in the "shellapi" unit.
- Drop a TComboBox on a form. Name it "cboPrinter". Set Style to csDropDownLidt
- Put the next two lines in the form's OnCreate even 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 function you can use to print any document type to a specified printer:
uses shellapi, printers;
procedure 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: if the selected printer is the default one, the function uses "print" action. If the selected printer is not the default one, the function uses the "printo" method.
Note, also: some document types do NOT have an application associated for printing. Some do not have the "printto" action specified.
- Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX, TXT
- Flash在线文档阅读器::pdf、doc、docx、xls、xlsx、ppt、pptx、htm、txt、rtf、epub、csv、xdoc等
- Flash在线文档阅读器::pdf、doc、docx、xls、xlsx、ppt、pptx、htm、txt、rtf、epub、csv、xdoc等
- PHP读取doc,docx,xls,pdf,txt内容
- VBA批量转换:docx转pdf、doc、rtf、txt以及doc转docx
- "doc", "docx", "xls", "xlsx", "ppt", "pptx",txt。等文件转化为pdf
- POI解析文档内容(txt,doc,docx,xls,xlsx,ppt,pdf)
- java IO流(1)处理txt、rtf、doc、docx
- docx、ppt、xls、pdf文件转html
- 预览 doc docx,xls,xlsx,pdf等API 说明文档
- Converting .docx into (.doc, .pdf, .html)
- 获取txt pdf doc docx类型文件内容
- lucene pdf+doc+ppt+xls+txt+多层文件
- 在线打开.PDF、.TXT等文件,.doc、.xls自动下载
- android 打开 ppt pptx doc docx xls xlsx pdf office 文档
- 在网页中在线查看文档(doc、docx 、xls 、xlsx、 pdf 、swf )
- Android中pdf,doc,docx,xls,xlsx,ppt,pptx等office文件预览
- Doc、Docx转成HTML
- C#教学第19讲字符1(学习笔记)
- 加速浏览器控件的创建
- 奇数阶魔方阵
- 应CSDN之邀参加SD2.0大会
- 协程与微线程——概念篇
- Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX, TXT
- LINK : fatal error LNK1104: 无法打开文件“C:/XXX.obj”
- 泩活 ㄚi 成卟変
- row.rowKeyStr
- 在载入页面前呼叫 DataControl Operations
- 如何进入有密码的WinXP系统
- 网络断掉,不能启动DBConsole服务的解决。
- Install bash on AIX
- 高效分布式数据库缓存经典解决方案