Convert Files with the Document Converter - WinForms C# .NET 6

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

Overview  
Summary This tutorial shows how to use LEADTOOLS Document Converters in a WinForms C# application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (5 KB)
Platform C# Windows WinForms Application
IDE Visual Studio 2022
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 - WinForms C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Display Files in the Document Viewer tutorial. If you do not 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). For this project, the following references are needed:

If NuGet references are used, this tutorial requires the following NuGet packages:

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 DLL files are required for your application, refer to Files to be Included in your Application.

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 Form1.cs. Right-click on the Design Window and select View Code, or press F7, to bring up the code behind the Form.

Update the using block at the top to become like the following:

C#
using System; 
using System.IO; 
using System.Drawing; 
using System.Windows.Forms; 
using Leadtools; 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Document.Viewer; 
using Leadtools.Controls; 
using Leadtools.Document.Converter; 

Create a new button that when clicked will call the conversion method. Add the following code at the end of the InitUI() method:

C#
   var convertButton = new Button(); 
   convertButton.Name = "convertButton"; 
   convertButton.Text = "&Convert"; 
   convertButton.Left = loadButton.Right + 10; 
   convertButton.Click += (sender, e) => ConvertDocument(convertButton); 
   topPanel.Controls.Add(convertButton); 

Add a new method to the Form1 class called ConvertToDocx to perform conversion with the LEADDocument inside the Document Viewer.

C#
// Add the function below in the Form1 class 
private void ConvertToDocx(string outputFile) 
{ 
   using (DocumentConverter documentConverter = new DocumentConverter()) 
   { 
      var format = Leadtools.Document.Writer.DocumentFormat.Docx; 
      var jobData = DocumentConverterJobs.CreateJobData(documentViewer.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 a new method to the Form1 class called ConvertDocument(Button convertButton). This method will be called when the convertButton is clicked. Call the ConvertToDocx method from within the ConvertDocument method:

C#
private void ConvertDocument(Button convertButton) 
{ 
   if (documentViewer.Document == null) 
   { 
      MessageBox.Show("Unable to convert! Please load a document first"); 
      return; 
   } 
   try 
   { 
      SaveFileDialog saveDlg = new SaveFileDialog(); 
      saveDlg.Filter = "Word DOCX|*.docx"; 
      if (saveDlg.ShowDialog(this) != DialogResult.OK) 
         return; 
      ConvertToDocx(saveDlg.FileName); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.ToString()); 
   } 
} 

Run the Project

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

If the steps were followed correctly, the application should run and after loading a document into the Document Viewer, the Convert.. menu item should enable saving that document into a new MS Word(DOCX) file.

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 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.