Print to PDF and other Document Formats using Virtual Printers

Paperless, or paper-free, offices are getting more popular in all work environments. Because of this, digital printing is becoming more of the norm. With the use of the LEADTOOLS Document Writer and Virtual Printer, you can create a virtual printer and print to a PDF from any printable source. What makes this great is the simplicity of configuring the printer. Now let’s get to the code!

Configure the Printer

Configure the printer information using the PrinterInfo Class Properties and then install.


string printerName = "Custom Virtual Printer"; 
string printerPassword = "Test Password"; 
string printerRegistryKey = $@"SOFTWARE\LEAD Technologies, Inc.\20\Printer\{printerName}";

PrinterInfo printerInfo = new PrinterInfo
{
   MonitorName = printerName,
   PortName = printerName,
   ProductName = printerName,
   PrinterName = printerName,
   Password = printerPassword,
   RegistryKey = printerRegistryKey,
   RootDir = @"C:\LEADTOOLS 20\Bin\Common\PrinterDriver",
   Url = "https://www.leadtools.com",
   PrinterExe = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath,
   AboutString = "LEADTOOLS Printer",
   AboutIcon = @"C:\LEADTOOLS 20\Examples\Resources\RasterPro.ico"
};

Printer.Install(printerInfo);

Listen for Events

Once installed, you will need to listen for print events and then handle the print jobs. This is where the DocumentWriter will come into play. Feel free to change the DocumentFormat from PDF to another format.


// Save to PDF with optional PDF-A PdfOptions
DocumentFormat outputFormat = DocumentFormat.Pdf;

DocumentWriter documentWriter = new DocumentWriter();

if (documentWriter.GetOptions(DocumentFormat.Pdf) is PdfDocumentOptions pdfOptions)
{
   // Optional: Save to PDF-A
   pdfOptions.DocumentType = PdfDocumentType.PdfA;
   documentWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);
}

// Create/configure the Printer
Printer printer = new Printer(printerName);
printer.UnLock(printerPassword);

printer.EmfEvent += Printer_EmfEvent;
printer.JobEvent += Printer_JobEvent;

Emf and Job Event Code

The EmfEvent provides the listening application with EMF data for each print job page received by the Virtual Printer and the JobEvent provides the listening application with print job information for each print job received by the Virtual Printer.


private static void Printer_EmfEvent(object sender, EmfEventArgs e)
{
   using (Metafile metaFile = new Metafile(e.Stream))
      documentWriter.AddPage(new DocumentWriterEmfPage()
      {
         EmfHandle = metaFile.GetHenhmetafile()
      });
}

private static void Printer_JobEvent(object sender, JobEventArgs e)
{
   string ext = DocumentWriter.GetFormatFileExtension(outputFormat);
   if (ext.StartsWith("."))
      ext = ext.Substring(1);
      
   string outputDir = @"C:\temp\Output";
   string outputFile = Path.Combine(outputDir, $"{e.JobID}.{ext}");

   switch (e.JobEventState)
   {
      case EventState.JobStart:
         documentWriter.BeginDocument(outputFile, outputFormat);
         break;
      case EventState.JobEnd:
         documentWriter.EndDocument();
         break;     
   }
}

Check out the GIF below to see the application in action

If you come across any questions regarding our Virtual Printer SDK, feel free to comment on this post or reach out to our Technical Support Team at Support@Leadtools.com.

About 

Technical Marketing Engineer

This entry was posted in Document Imaging and tagged . Bookmark the permalink.

Leave a Reply

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