Save Documents with the Document Converter - WinForms C#

This tutorial shows how to create a C# WinForms application that saves files loaded in a LEADDocument class as raster image format or document format with searchable text using the Document Converter class.

Overview  
Summary This tutorial covers how to use LEADTOOLS Document Converter SDK technology in a C# WinForms application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (9 KB)
Platform C# WinForms Application
IDE Visual Studio 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a Document Viewer project by reviewing the Add References and Set a License and Display Files in Document Viewer tutorials, before working on the Save Documents with the Document Converter - WinForms C# tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Display Files in Document Viewer tutorial. If you don't have that project, follow the steps in that tutorial to create it.

Ensure the project has the necessary LEADTOOLS references mentioned in the original tutorial.

If using NuGet references, this tutorial needs the NuGet packages already required by the original tutorial.

Alternatively, if local DLL references are used, the following DLLs are needed in addition to the DLLs in the original. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Initialize DocumentConverter Class

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Form1.cs. Right-click on the Design Window and select View Code or press F7 to bring up the code behind the Form. Add the following statements to the using block at the top:

Add the following code to the using block:

C#
// Using block at the top 
using Leadtools.Document.Converter; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 
using Leadtools.Codecs; 

Set DocumentConverter and ocrEngine as global variables:

C#
private DocumentConverter documentConverter; 
private IOcrEngine ocrEngine; 

Add a call to a InitDocumentConverter() method in the Form1() code:

C#
public Form1() 
{ 
   InitializeComponent(); 
   SetLicense(); 
   InitDocumentViewer(); 
   InitDocumentConverter(); 
} 

Use the code below to initialize the DocumentConverter and configure the OCR engine to be used when converting to a document format with searchable text.

C#
private void InitDocumentConverter() 
{ 
   documentConverter = new DocumentConverter(); 
   // Set OCR Engine 
   ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD); 
   ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
   documentConverter.SetOcrEngineInstance(ocrEngine, false); 
   // Set Document Writer 
   documentConverter.SetDocumentWriterInstance(new DocumentWriter()); 
} 

Add the Save Buttons

Add the code below to the InitUI() to add two buttons, one button for saving as image, another for saving as document.

C#
var saveAsTifButton = new Button(); 
saveAsTifButton.Name = "saveAsTifButton"; 
saveAsTifButton.Text = "&Save As Image"; 
saveAsTifButton.Width = loadButton.Width * 2; 
saveAsTifButton.Location = new System.Drawing.Point(loadButton.Location.X + loadButton.Width, loadButton.Location.Y); 
saveAsTifButton.Click += (sender, e) => ConvertToImage(RasterImageFormat.Tif); 
topPanel.Controls.Add(saveAsTifButton); 
 
var saveAsPDFButton = new Button(); 
saveAsPDFButton.Name = "saveAsPDFButton"; 
saveAsPDFButton.Text = "&Save As PDF"; 
saveAsPDFButton.Width = loadButton.Width * 2; 
saveAsPDFButton.Location = new System.Drawing.Point(saveAsTifButton.Location.X + saveAsTifButton.Width, saveAsTifButton.Location.Y); 
saveAsPDFButton.Click += (sender, e) => ConvertToDocument(DocumentFormat.Pdf); 
topPanel.Controls.Add(saveAsPDFButton); 

Add the Convert to Image Code

Use the code below to set the method used for converting the loaded document to a raster image format and save it on disk.

C#
private void ConvertToImage(RasterImageFormat imageFormat) 
{ 
   if (documentViewer.Document != null) 
   { 
      SaveFileDialog saveAsImageDlg = new SaveFileDialog(); 
      saveAsImageDlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
      saveAsImageDlg.Filter = $"{imageFormat}|*.{RasterCodecs.GetExtension(imageFormat)}"; 
      if (saveAsImageDlg.ShowDialog(this) != DialogResult.OK) 
         return; 
 
      DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(documentViewer.Document, saveAsImageDlg.FileName, imageFormat); 
      jobData.JobName = "Convert to Image Job"; 
 
      DocumentConverterJob job = documentConverter.Jobs.CreateJob(jobData); 
      documentConverter.Jobs.RunJob(job); 
 
      if (job.Errors.Count > 0) 
         foreach (var error in job.Errors) 
            MessageBox.Show($"Error during conversion: {error.Error.Message}\n"); 
      else 
         MessageBox.Show($"Successfully Saved Document to {saveAsImageDlg.FileName}\n"); 
   } 
   else 
      MessageBox.Show("Please Load Document First"); 
} 

Add the Convert to Document Code

Use the code below to set the method used for converting the loaded file to a document format and save it on disk.

C#
private void ConvertToDocument(DocumentFormat documentFormat) 
{ 
   if (documentViewer.Document != null) 
   { 
      SaveFileDialog saveAsDocDlg = new SaveFileDialog(); 
      saveAsDocDlg.InitialDirectory = @"C:\LEADTOOLS22\Resources\Images"; 
      saveAsDocDlg.Filter = $"{documentFormat}|*.{DocumentWriter.GetFormatFileExtension(documentFormat)}"; 
      if (saveAsDocDlg.ShowDialog(this) != DialogResult.OK) 
         return; 
 
      DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(documentViewer.Document, saveAsDocDlg.FileName, documentFormat); 
      jobData.RasterImageFormat = RasterImageFormat.Unknown; 
      jobData.JobName = "Convert to PDF Job"; 
 
      DocumentConverterJob job = documentConverter.Jobs.CreateJob(jobData); 
      documentConverter.Jobs.RunJob(job); 
 
      if (job.Errors.Count > 0) 
         foreach (var error in job.Errors) 
            MessageBox.Show($"Error during conversion: {error.Error.Message}\n"); 
      else 
         MessageBox.Show($"Successfully Saved Document to {saveAsDocDlg.FileName}\n"); 
   } 
   else 
      MessageBox.Show("Please Load Document First"); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and allows saving any loaded document to either a TIF image or a PDF with searchable text.

Wrap-up

This tutorial showed how to convert files loaded in a DocumentViewer control to raster image formats and document formats. Also it covered how to use the DocumentConverter class.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.