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 | 15 minutes |
Visual Studio Project | Download tutorial project (9 KB) |
Platform | C# Windows WPF Application |
IDE | Visual Studio 2019, 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
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.
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:
Leadtools.Document.Sdk
Leadtools.Document.Viewer.Wpf
If using local DLL references, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Caching.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Tif.dll
Leadtools.Controls.Wpf.dll
Leadtools.Document.dll
Leadtools.Document.Converter.dll
Leadtools.Document.Pdf.dll
Leadtools.Document.Raster.dll
Leadtools.Document.Viewer.Wpf.dll
Leadtools.Document.Writer.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
Leadtools.Pdf.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
With the project created, the references added, the license set, the Document Viewer initialized, and the load document code added, 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:
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.
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 handler, as shown below. This method will be called when the Convert
MenuItem is clicked.
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 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:
Click on File -> Open to bring up the OpenFileDialog.
Select an image to be loaded into the ImageViewer.
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.
In this tutorial, we covered how to use the LEADDocument
class with the DocumentConverter
class to perform simple conversions using the default settings.