Tutorial: Print to Any Type of File Using the Virtual Printer Driver

With a global focus on optimizing paperless office systems, virtual printing solutions have never been in higher demand. A Virtual Printer is a simulated device that simulates a physical printer but instead of printing on paper, it gives access to the file in memory and processes the pages so that users can save it to file or transmit it via other means such as email or to a database. Using the LEADTOOLS Virtual Printer SDK, developers can create virtual printer solutions to host locally or on a networked server to allow multiple users to access it. With LEADTOOLS support for over 150 file formats, users can save these virtual print jobs to just about any format.

The below code shows off the basics of what is needed to create a virtual printer solution. If you want a complete step-by-step tutorial, check out our Print to File Using the Virtual Printer Driver tutorial.

static Printer LeadPrinter;
static DocumentWriter DocumentWriter;
static PrinterInfo PrinterInfo;
static string OutputFile;

static void SetupPrinter()
    List<string> installedPrinters = new List<string>();

    foreach (string printer in PrinterSettings.InstalledPrinters)

    string printerName = "LEADTOOLS Printer";
    string printerPassword = "";
    string documentPrinterRegPath = @"SOFTWARE\LEAD Technologies, Inc.\21\Printer";
    PrinterInfo = new PrinterInfo
        MonitorName = printerName,
        PortName = printerName,
        ProductName = printerName,
        PrinterName = printerName,
        Password = printerPassword,
        RegistryKey = documentPrinterRegPath + printerName,
        RootDir = @"C:\LEADTOOLS21\Bin\Common\PrinterDriver",
        Url = "https://www.leadtools.com",
        PrinterExe = AppDomain.CurrentDomain.BaseDirectory

	// Install the Printer
    if (!installedPrinters.Contains(printerName))

	// Create Job Event Handlers
    LeadPrinter = new Printer(printerName);
    LeadPrinter.EmfEvent += new EventHandler<EmfEventArgs>(LeadPrinter_EmfEvent);
    LeadPrinter.JobEvent += new EventHandler<JobEventArgs>(LeadPrinter_JobEvent);

static void LeadPrinter_EmfEvent(object sender, EmfEventArgs e)
    Metafile metaFile = new Metafile(e.Stream);

    DocumentWriterEmfPage documentPage = new DocumentWriterEmfPage
        EmfHandle = metaFile.GetHenhmetafile()

static void LeadPrinter_JobEvent(object sender, JobEventArgs e)
    string printerName = e.PrinterName;
    int jobID = e.JobID;

    if (e.JobEventState == EventState.JobStart)
        OutputFile = Path.Combine(@"C:\Temp", Path.ChangeExtension(Path.GetRandomFileName(), "pdf"));
        DocumentWriter.BeginDocument(OutputFile, DocumentFormat.Pdf);

        Console.WriteLine($"Job {jobID} for {printerName} was started");
    else if (e.JobEventState == EventState.JobEnd)

        Console.WriteLine($"Job {jobID} for {printerName} was ended. PDF saved to {OutputFile}");

Try it out!

To test this for yourself, make sure to get the latest LEADTOOLS SDK code for free straight from our site if you have not already. This trial is good for 60 days and comes with unlimited chat and email support.


Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.


Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in Virtual Printer. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *