How to create Virtual Printer using Delphi
Examples
◊ Sample Client application
used for testing and development purposes.
This application demonstrates how to use an INI file to write print
job information and paths to the generated files.
Download Sample Client Application:
Virtual Printer for Delphi
◊ Print Previewer
- a sample application demonstrating the features of the virtual printer.
Print Previewer, which may be used in custom client applications, provides
detailed information about printed files and displays the EMF files inside a
form. The sample contains code that outputs the metafile to the screen.
Download Print Previewer:
Virtual Printer for Delphi
◊ Printer Configuration
- a sample utility for configuring the virtual printer. This utility permits
setting such parameters as the client application path, transfer mode of the
INI file path, and the folder to save the generated files. (The printer may
print to EMF, PDF, TXT, JPEG, TIFF, or BMP files.)
Download Printer Configuration:
Virtual Printer for Delphi
◊ How to print files programmatically using the ShellExecute function
How to print files programmatically using the ShellExecute function
The code sample below demonstrates how to print files programmatically on either a physical or a virtual printer using the ShellExecute function. It also demonstrates how to change the default system printer.
function GetDefaultPrinter(szPrinter:PAnsiChar; var bufferSize:DWORD):
BOOL; stdcall; external 'winspool.drv' name 'GetDefaultPrinterA';
function SetDefaultPrinter(szPrinter:PAnsiChar):
BOOL; stdcall; external 'winspool.drv' name 'SetDefaultPrinterA';
procedure PrintDocumentUsingShellExecute (szPrinter: String, szDocumentPath: String)
var
szDefaultPrinter, szNamePrinterBuff: String;
bufferSize: DWord;
begin
// get the default printer
SetLength(szDefaultPrinter, MAX_PATH);
bufferSize:= MAX_PATH;
GetDefaultPrinter(Pchar(szDefaultPrinter), bufferSize);
szDefaultPrinter:= String (PChar(szDefaultPrinter));
//change the default printer
if(szPrinter <> szDefPrinter)
then SetDefaultPrinter(PChar(szPrinter));
// send the document to the print
ShellExecute(0, 'print', PChar(szDocumentPath), nil, nil, SW_HIDE);
//set default printer back to original
if(szPrinter <> szDefaultPrinter)
then SetDefaultPrinter(PChar(szDefaultPrinter));
end;
Then it's necessary to call these functions with the required parameters. For example, you can print MS Word and PDF documents this way:
PrintDocumentUsingShellExecute('Your Virtual Printer', 'c:\Documents\AnyDocument.doc')
PrintDocumentUsingShellExecute('Your Virtual Printer', 'c:\ Documents \AnyDocument.pdf')