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 (4 KB) |
Platform | C# WinForms Application |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
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.
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 using NuGet references, this tutorial needs the NuGet packages already required by the original tutorial.
Alternatively, if local DLL references are used, the following DLLs are needed in addition to the DLLs in the original. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\net
:
Leadtools.Document.Converter.dll
Leadtools.Document.Raster.dll
Leadtools.Document.Writer.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
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 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, 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:
// Using block at the top
using Leadtools.Document.Converter;
using Leadtools.Document.Writer;
using Leadtools.Ocr;
using Leadtools.Codecs;
Set DocumentConverter
and ocrEngine
as global variables:
private DocumentConverter _documentConverter;
private IOcrEngine _ocrEngine;
Add a call to a InitDocumentConverter()
method in the Form1()
code:
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.
private void InitDocumentConverter()
{
_documentConverter = new DocumentConverter();
// Set OCR Engine
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
_ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime");
_documentConverter.SetOcrEngineInstance(_ocrEngine, false);
// Set Document Writer
_documentConverter.SetDocumentWriterInstance(new DocumentWriter());
}
Add the code below to the InitUI()
to add two buttons, one button for saving as image, another for saving as document.
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);
Use the code below to set the method used for converting the loaded document to a raster image format and save it on disk.
private void ConvertToImage(RasterImageFormat imageFormat)
{
if (documentViewer.Document != null)
{
SaveFileDialog saveAsImageDlg = new SaveFileDialog();
saveAsImageDlg.InitialDirectory = @"C:\LEADTOOLS22\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");
}
Use the code below to set the method used for converting the loaded file to a document format and save it on disk.
private void ConvertToDocument(DocumentFormat documentFormat)
{
if (_documentViewer.Document != null)
{
SaveFileDialog saveAsDocDlg = new SaveFileDialog();
saveAsDocDlg.InitialDirectory = @"C:\LEADTOOLS22\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 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.
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.