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

Programmatically Print Any Type of Document Using Delphi and ShellExecute

Print Documents From Delphi

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.

Here's how to Change the Default Windows Printer from Delphi Code

Delphi tips navigator:
» Convert / Format an Amount of Microseconds into a TDateTime Value
« Get Selected Tabs of a Multiselect TTabControl in Delphi