Limited time promo

CSharp Sample - CSharp Converter

English

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

Download now Buy licenses
Mar 28, 2022
novaPDF 11.x
We strive to keep our help 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 SDK 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.
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 SDK using the ShellExecute function call.
When using this technique to convert a file to PDF, you have to take care of 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 SDK before calling ShellExecute (using the SetDefaultPrinter method), wait the process to be started (using WaitForExit()), restore the default printer (with the RestoreDefaultPrinter method). This way you made sure that the default printer was restored and your document is printed to novaPDF SDK.

Source code snippets


1. DECLARE INovaPdfOptions variable
 private NovaPdfOptions11Class mobjNovaOptios ;
2. Initialize INovaPdfOptions
mobjNovaOptios = new NovaPdfOptions<%VERSION_MAJOR%>Class();

  // initialize the NovaPdfOptions object
      mobjNovaOptios.Initialize(PRINTER_NAME, "");

      //create option profiles
  AddSmallProfile();
  AddFullProfile();
3. Set novaPDF SDK Options
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);
  }
}
4. Start a print job
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();
}