Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Friday, March 1, 2019 4:38:21 PM(UTC)
Anthony Northrup

Groups: Registered, Tech Support, Administrators
Posts: 199

Was thanked: 28 time(s) in 28 post(s)

Whether you're browsing the web, or within an application like Microsoft Word, printing to PDF is very useful functionality. With the use of our Document Writer and Virtual Printer functionality this is simple to configure. Attached is a complete .NET Framework console application which uses the v20 LEADTOOLS SDK to enable users to print to PDF. The demo can also be easily modified to print to other formats such as DOCX or XLSX by updating the OutputFormat variable on line 18 (line 44 of the snippet below).

Below is a simplified demonstration on how this functionality is implemented:
Code:

// Step 0: Setup variables
string printerName = "Print to PDF";
string outputFolder = @"C:\temp";
DocumentWriter documentWriter = new DocumentWriter();

// Step 1: Install the printer
Printer.Install(new PrinterInfo()
{
   MonitorName = printerName,
   PortName = printerName,
   ProductName = printerName,
   Password = null, // No password
   RegistryKey = $@"SOFTWARE\LEAD Technologies, Inc.\20\Printer\{printerName}",
   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"
});

// Step 2: Listen for print events
Printer printer = new Printer(printerName);
printer.EmfEvent += Printer_EmfEvent;
printer.JobEvent += Printer_JobEvent;

// Step 3: Handle the print jobs
void Printer_EmfEvent(object sender, EmfEventArgs e)
{
   // Load the data stream into a System.Drawing.Imaging.Metafile object
   using (Metafile metaFile = new Metafile(e.Stream))
      // Add to the current document
      documentWriter.AddPage(new DocumentWriterEmfPage()
      {
         EmfHandle = metaFile.GetHenhmetafile()
      };
}
void Printer_JobEvent(object sender, JobEventArgs e)
{
   switch (e.JobEventState)
   {
      case EventState.JobStart:
         // Begin writing to a new file
         documentWriter.BeginDocument(
            Path.Combine(outputFolder, $"{e.JobID}.pdf"), DocumentFormat.Pdf
         );
         break;
      case EventState.JobEnd:
         // Finished with file
         documentWriter.EndDocument();
         break;
   }
}

File Attachment(s):
PrintToPDF.zip (5kb) downloaded 184 time(s).

Edited by moderator Thursday, March 7, 2019 1:55:38 PM(UTC)  | Reason: Not specified

Anthony Northrup
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.079 seconds.