Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX, TXT

本文介绍如何使用Delphi编程语言实现打印不同类型的文件,包括PDF、DOC等格式。通过调用ShellExecute函数并利用Windows系统的关联应用程序来完成打印任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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.

  1. Drop a TComboBox on a form. Name it "cboPrinter". Set Style to csDropDownLidt
  2. 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值