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 (8 KB) |
Platform | C# Windows WinForms Application |
IDE | Visual Studio 2017, 2019 |
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 - WinForms C# tutorial.
In Visual Studio, create a new C# Windows WinForms project, and add the below necessary LEADTOOLS references.
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:
Leadtools.Document.Sdk
Leadtools.Document.Viewer.WinForms
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Caching.dll
Leadtools.Codecs.dll
Leadtools.Controls.WinForms.dll
Leadtools.Document.dll
Leadtools.Document.Pdf.dll
Leadtools.Document.Viewer.WinForms.dll
Leadtools.Document.Converter
Leadtools.Document.Writer
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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.
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:
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:
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.
// 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:
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 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.
In this tutorial, we covered how to use the LEADDocument
class with the DocumentConverter
class to perform simple conversion using the default settings.