Save Documents with the Document Converter - WinForms C# .NET 6

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 (8 KB)
Platform C# WinForms Application
IDE Visual Studio 2022
Development License Download LEADTOOLS

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 NuGet references are used, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

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 Leadtools; 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Document.Viewer; 
using Leadtools.Controls; 
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:\LEADTOOLS23\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:\LEADTOOLS23\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:\LEADTOOLS23\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 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

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