Wednesday, November 6, 2019
Print Any Type of Document Using Delphi
Print Any Type of Document Using 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 can easily print documents created in that program. For example, Microsoft Word saves the text you write in documents with DOC extension. Since Word 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? Print From Delphi We can ask Windows what application can print, for example, a PDF file. Or, even better, we can tell Windows, heres one PDF file, send it to the application associated / in charge of printing PDF files. To do this, 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, thats 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 However, ShellExecute can do much more. ShellExecute can be used to launch an application, open Windows Explorer, initiate a search beginning in the specified directory, and- whats of greatest interest to us- print the specified file. Specify Printer 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 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. The 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 csDropDownLidtPut the next two lines in the forms OnCreate even handler: //have available printers in the combo boxcboPrinter.Items.Assign(printer.Printers);//pre-select the default / active printercboPrinter.ItemIndex : printer.PrinterIndex; use to print any document type to a specified printer Note: some document types do not have an application associated with printing. Some do not have the printto action specified.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment