CSharp Converter - SDK sample

Download Sample
Download size: 47.3KB
[Format: ZIP]
Note: To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: nova PDF SDK.

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.

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

1. Declare INovaPdfOptions variable 

private NovaPdfOptionsClass mobjNovaOptios; 

2. Initialize INovaPdfOptions 

mobjNovaOptios = new NovaPdfOptionsClass(); 

// initialize the NovaPdfOptions object
// if you have an application license for novaPDF SDK,
// pass both the registration name and the license key to the Initialize() function
// mobjNovaOptios.Initialize(PRINTER_NAME, "<registration name>", "<license key>");mobjNovaOptios.Initialize(PRINTER_NAME, "", "", ""); 

AddSmallProfile();
AddFullProfile(); 

3. Set novaPDF Printer Options 

try
{
  mobjNovaOptios.AddProfile2(SMALL_SIZE_PROFILE);
  // Set some options to this profile
  // disable the "Save PDF file as" prompt
  mobjNovaOptios.SetOptionLong2(NovaOptions.NOVAPDF_SAVE_PROMPT, 0, SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC);
  // set generated Pdf files destination folder  "c:\"
  mobjNovaOptios.SetOptionString2(NovaOptions.NOVAPDF_SAVE_FOLDER, "c:\\", SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC); 

  // ..... 

}
catch(System.Runtime.InteropServices.COMException ComException)
{
  if (((~ComException.ErrorCode ^ NovaErrors.NV_PROFILE_EXISTS) ^ 0xFFFFFFFF)==0)
  {
    System.Diagnostics.Debug.WriteLine("Profile \"Small Size Profile\" exists");
  }
  else
  {
    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();
mobjNovaOptios.SetActiveProfile2((string)(cmbProfiles.SelectedItem));
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.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();
}

© 2002-2008 Softland. All Rights Reserved.
PDF Creator novaPDF
Create PDF for your paperless office
home Home   contact usContact us   partnershipsPartnerships   bookmarkBookmark   Site updated: 14-August-2008
Downloads Download Now

Download, install and use novaPDF. Latest version: 5.4 build 270

Download PDF creator
promotionsSpecial Offers

Current discounts:
- up to 25% for Desktop Editions
- up to 25% for Server Editions
- 20% Academic Discount
- up to 16% for Developer Tools

buyBuy Now
Professional edition $39.95
(Save $10)
Standard edition $29.95
(Save $10)
Lite edition $19.95
(Save $5)
Buy Now
newsLatest News

novaPDF 5.4 released
February 11, 2008

What’s new (read more):
 » Added Copy Text/Image Watermarks dialog
 » Added "Lookup Address" option when sending emails
 » Corrected rotated and vertical texts conversion
 » Corrected conversion from WordArt, MS Word backgrounds and MS Visio drawings
 » Uninstaller deletes now the driver files

sdkDeveloper Tools

novaPDF SDK is a software development toolkit that can be used by software developers and programmers to add the ability to create PDF files in their applications.
novaPDF OEM is a PDF solution that programmers can use to generate PDF files that will be post-processed by their applications. […]

Reviews & User QuotesPDF Creator Reviews
"novaPDF is a really neat tool. Basically, it’s a PDF printer driver that prints to a PDF file instead of paper.
See more reviews about nova PDF
[…] novaPDF is a great product that works without forcing the user to configure anything. The convenience alone is worth the money." […]
Jason Pullara, Editor
awardsAwards

novaPDF received the Editor’s Pick from Soft32, a major download site:

PDF creator reviews

See the Awards page for more ratings.

Currently supported languagesLanguages

News:
- Norwegian language added.

Help us translate novaPDF and we will give you licenses for novaPDF Professional and mention you on the Languages page. […]

NewsletterKnowledgebase articles

Latest articles on how to:
 » Create PDF files
 » Convert JPG to PDF
 » Convert Powerpoint to PDF
 » Convert Publisher to PDF
 » Convert web pages to PDF
Full article list in the knowledgebase.