Limited time promo

CSharp Converter - SDK sample

English en

This article applies only to novaPDF. If you don't have it yet, you must download it first.

Download now Buy licenses
Feb 25, 2019
NovaPDF 9.x
NovaPDF 8.x
We strive to keep our articles as accurate as possible. If you notice any inconsistencies or outdated info please let us know.
The CSharp Converter sample demonstrates how to convert an existing file by printing it to novaPDF Printer using the ShellExecute function. It also demonstrates how to set different options and manage profiles.
The same approach should be taken if you print using a Print() method from another object (like an internet browser or a report control). Just replace the ShellExecute call with the call of your Print method.
To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: nova PDF SDK

When the application starts, it creates a few profiles and makes different settings in the profiles. Then it shows a dialog from where the user can select the active profile and change its settings using the controls from the dialog. After that a document can be selected from the harddisk and printed to novaPDF Printer using the ShellExecute function call.
When using this technique to convert a file to PDF, you have to consider the fact that ShellExecute prints to the default printer. This function returns immediately and does not wait until the print is finished (it may return before the printing is actually started). Therefore you have to set the default printer to novaPDF Printer before calling ShellExecute (using the SetDefaultPrinter method), wait the process to be started (using WaitForExit()) and restore the default printer (with the RestoreDefaultPrinter method). This way you make sure that the default printer was restored and your document is printed to novaPDF Printer.
Source Code Snippets
  • DECLARE INovaPdfOptions variable
cs private NovaPdfOptions80Class mobjNovaOptios;
  • Initialize INovaPdfOptions
cs mobjNovaOptios = new NovaPdfOptions80Class(); // initialize the NovaPdfOptions object mobjNovaOptios.Initialize(PRINTER_NAME, ""); //create option profiles AddSmallProfile(); AddFullProfile();
  • Set <%SDKSAMPLEPRINTER%> Options
cs try { String newSmallSizeProfileId = String.Empty; // Set some options to this profile mobjNovaOptios.AddProfile(SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC, out newSmallSizeProfileId); //load the new profile mobjNovaOptios.LoadProfile(newSmallSizeProfileId); // disable the "Save PDF file as" prompt mobjNovaOptios.SetOptionLong(NovaOptions.NOVAPDF_SAVE_PROMPT_TYPE, 0); // set generated Pdf files destination folder "c:\" mobjNovaOptios.SetOptionString(NovaOptions.NOVAPDF_SAVE_FOLDER, "c:\\"); // ..... } catch (System.Runtime.InteropServices.COMException ComException) { MessageBox.Show("Error creating Small Size Profile:\r\n" + ComException.Message); System.Diagnostics.Debug.WriteLine(ComException.Message); } }
  • Start a print job
cs private void btnStartPrinting_Click(object sender, System.EventArgs e) { UpdateProfileFromDialog(); if (txtFileToConvert.Text.Trim() == "") { MessageBox.Show("No file to convert!"); return; } mobjNovaOptios.SetActiveProfile((string)(cmbProfiles.SelectedItem.ToString())); mobjNovaOptios.SetDefaultPrinter(); mobjNovaOptios.LicenseShellExecuteFile(txtFileToConvert.Text); Process myProcess = new Process(); try { myProcess.StartInfo.FileName = txtFileToConvert.Text; myProcess.StartInfo.Verb = "Print"; myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; myProcess.Start(); } catch (Win32Exception ex) { if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND) { Console.WriteLine(ex.Message + ". Check the path and filename"); } else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED) { // Note that if your word processor might generate exceptions // such as this, which are handled first. Console.WriteLine(ex.Message + ". You do not have permission to print this file."); } } myProcess.WaitForExit(10000); myProcess.Close(); mobjNovaOptios.RestoreDefaultPrinter(); }