Convert Files with the Document Converter - WPF C#

This tutorial shows how to perform document conversion in a simplified manner using the LEADTOOLS SDK in a WPF C# application.

Overview  
Summary This tutorial shows how to use LEADTOOLS Document Converters in a WPF C# application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (9 KB)
Platform C# Windows WPF Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

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

Create the Project and Add LEADTOOLS References

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

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet packages:

If using local DLL references, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\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 local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Convert Document Code

With the project created, the references added, the license set, the Document Viewer initialized, and the load document to viewer code add, coding can begin.

In the Solution Explorer, open MainWindow.xaml. Add the below XAML code to add a new menu item to the Menu, below the Load menu item:

<MenuItem Name="_fileConvert" Header="Convert" Click="_fileConvert_Click"/> 

Right-click in the XAML Window and select View Code, or press F7, to bring up the code behind the window. Ensure the following using statements are in the using block at the top:

C#
using System; 
using System.IO; 
using System.Drawing; 
using Microsoft.Win32; 
using Leadtools; 
using Leadtools.Caching; 
using Leadtools.Controls; 
using Leadtools.Document; 
using Leadtools.Document.Converter; 
using Leadtools.Document.Viewer; 

Add a new method to the MainWindow.xaml.cs class called ConvertToDocx to perform conversion with the LEADDocument inside the DocumentViewer. Add the below code into the new method.

C#
private void ConvertToDocx(string outputFile) 
{ 
   using (DocumentConverter documentConverter = new DocumentConverter()) 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      DocumentWriter documentWriter = new DocumentWriter(); 
      ocrEngine.Startup(null, null, null, null); 
      documentConverter.SetOcrEngineInstance(ocrEngine, false); 
      documentConverter.SetDocumentWriterInstance(documentWriter); 
      var format = DocumentFormat.Docx; 
      var jobData = DocumentConverterJobs.CreateJobData(docViewer.Document, outputFile, format); 
      jobData.JobName = "conversion job"; 
      var job = documentConverter.Jobs.CreateJob(jobData); 
      documentConverter.Jobs.RunJob(job); 
      if (job.Status == DocumentConverterJobStatus.Success) 
         MessageBox.Show("Success"); 
      else 
      { 
         MessageBox.Show($"{job.Status} Errors"); 
         foreach (var error in job.Errors) 
            MessageBox.Show($"{error.Operation} at {error.InputDocumentPageNumber}: {error.Error.Message}"); 
      } 
   } 
} 

Add the below code inside the _fileConvert_Click event handler. Call the ConvertToDocx(string outputFile) method above in this event hanlder, as shown below. This method will be called when the Convert MenuItem is clicked.

C#
private void _fileConvert_Click(object sender, RoutedEventArgs e) 
{ 
   if (docViewer.Document == null) 
   { 
      MessageBox.Show("Unable to convert! Please load a document first"); 
      return; 
   } 
   try 
   { 
      SaveFileDialog saveDlg = new SaveFileDialog(); 
      saveDlg.Filter = "Word DOCX|*.docx"; 
      var result = saveDlg.ShowDialog(this); 
      if (!result.HasValue || !result.Value) 
         return; 
      ConvertToDocx(saveDlg.FileName); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Run the Project

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

If the steps were followed correctly, the application should run and display that the license was set properly. To test, follow the steps below:

  1. Click on File -> Open to bring up the OpenFileDialog.

  2. Select an image to be loaded into the ImageViewer.

  3. Click on File -> Convert to bring up the SaveFileDialog. Choose your directory and file name you want to export the file to, and press Save to export to DOCX.

Wrap-up

In this tutorial, we covered how to use the LEADDocument class with the DocumentConverter class to perform simple conversion using the default settings.

See Also

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


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