How to create Virtual Printer using C#
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.
Read more about Sample Client Application
Sample Client Application for C#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.
Read more about Print Previewer
Download Print Previewer for C#Collecting Multiple Documents - in this example we'll create a more powerful client which will be able to collect files from multiple printed documents into collection and transfer them in a zip archive to a remote machine using ftp or http protocols.
Read more about Collecting Multiple Documents
Download Collecting Multiple Documents for C#/C++, VB6/VB.NET
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.
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GetDefaultPrinter(StringBuilder szPrinter,
ref int bufferSize);
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string szPrinter);
void PrintDocumentUsingShellExecute (string szPrinter, string DocumentPath)
{
StringBuilder szDefaultPrinter = new StringBuilder(256);
int bufferSize = szDefaultPrinter.Capacity;
//get the default printer
GetDefaultPrinter(szDefaultPrinter, ref bufferSize);
// change the default printer
if (String.Compare(szPrinter, szDefaultPrinter.ToString(), true) != 0)
{
SetDefaultPrinter(szPrinter);
}
// send the document to the print
Process printProcess = new Process();
printProcess.StartInfo.FileName = myDocument;
printProcess.StartInfo.Verb = "Print";
printProcess.StartInfo.CreateNoWindow = true;
printProcess.Start();
// set default printer back to original
if (String.Compare(szPrinter, szDefaultPrinter, true) != 0)
{
SetDefaultPrinter(szDefaultPrinter);
}
}
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")
Virtual Printer menu
Latest news
Send us your comments
We're really eager to hear from you. Please send us your suggestions in one of two ways:
- Write us via On-line form
- Add your comments to our Blog