This tutorial shows how to process patch codes in a document and apply the associated action in a C# Windows Console application using the LEADTOOLS SDK.
Note: When scanning a document with many pages, is best for organizational purposes to split the document into separate pages.
Overview | |
---|---|
Summary | This tutorial covers how to use the LEADTOOLS Barcode SDK technology to process patch codes in a C# .NET 6 application. |
Completion Time | 15 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET 6 Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or higher |
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 tutorial, before working on the Split Document with Patch Codes tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If that project is unavailable, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project.
This tutorial requires the following NuGet package:
Leadtools.Barcode
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, and the license set, coding can begin.
In the Solution Explorer, open Program.cs
. Add the following statements to the using block at the top.
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
Add the code below to the Main()
method to load each page of the given image. For the purposes of this tutorial you can use this sample image.
static void Main(string[] args)
{
try
{
string _inputFile = @"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf";
InitLEAD();
SplitPatchDocument(_inputFile);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
Add a new method called SplitPatchDocument()
and then call the new method in the Main()
method, as shown above. Add the below code to run patch code detection and split the document based on the results.
public static void SplitPatchDocument(string _inputFile)
{
using RasterCodecs _codecs = new RasterCodecs();
// Get page information
CodecsImageInfo _info = _codecs.GetInformation(_inputFile, true);
int fromPage = 1;
for (int _pageNum = 1; _pageNum <= _info.TotalPages; ++_pageNum)
{
// Load image page
RasterImage _image = _codecs.Load(_inputFile, _pageNum);
// Check for patch code
BarcodeEngine engine = new BarcodeEngine();
BarcodeReader barcodeReader = engine.Reader;
BarcodeData[] data = barcodeReader.ReadBarcodes(_image, LeadRect.Empty, 0, new BarcodeSymbology[] { BarcodeSymbology.PatchCode });
// If a patch code is present, change filename suffix to start writing to a new file
if (data.Length > 0)
fromPage = _pageNum;
else
{
// Append image page to current portion
string filename = string.Format("split_fromPage{0}.tif", fromPage);
string _splitDir = Path.Combine(@"C:\Temp\", filename);
_codecs.Save(_image, _splitDir, RasterImageFormat.Tif, 0, 1, -1, 1, CodecsSavePageMode.Append);
}
}
}
Note
Since Patch Codes are commonly used when scanning documents. It might be of interest to use the code for the
SplitPatchDocument(RasterImage image)
method with theAcquirePage
event handler of a TWAIN or WIA session. For more details, see the Acquire an Image From a TWAIN Source tutorial.
To handle the files using MemoryStream
, add a new method called SplitPatchDocumentStream
and modify the existing code in the Main()
method to call the new method:
static void Main(string[] args)
{
try
{
string _inputFile = @"C:\LEADTOOLS23\Resources\Images\Leadtools.pdf";
InitLEAD();
SplitPatchDocument(_inputFile);
// Uncomment below to handle file using Memory Stream
/*
byte[] _inputData = File.ReadAllBytes(_inputFile);
using (MemoryStream _inputStream = new MemoryStream(_inputData))
SplitPatchDocumentStream(_inputStream);
*/
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
public static void SplitPatchDocumentStream(Stream _inputStream)
{
using RasterCodecs _codecs = new RasterCodecs();
// Get page information
CodecsImageInfo _info = _codecs.GetInformation(_inputStream, true);
// Open output Memory Stream that will hold each split portion
MemoryStream _outputStream = new MemoryStream();
int fromPage = 1;
for (int _pageNum = 1; _pageNum <= _info.TotalPages; ++_pageNum)
{
// Load image page
RasterImage _image = _codecs.Load(_inputStream, _pageNum);
// Check for patch code
BarcodeEngine engine = new BarcodeEngine();
BarcodeReader barcodeReader = engine.Reader;
BarcodeData[] data = barcodeReader.ReadBarcodes(_image, LeadRect.Empty, 0, new BarcodeSymbology[] { BarcodeSymbology.PatchCode });
// If a patch code is present, change filename suffix to start writing to a new file
if (data.Length > 0)
{
// Use the output Memory Stream containing the intermediate portion here before it is cleared for the next portion
_outputStream.Dispose();
_outputStream.Close();
_outputStream = new MemoryStream();
fromPage = _pageNum;
}
else
{
// Append image page to current portion
_codecs.Save(_image, _outputStream, RasterImageFormat.Tif, 0, 1, -1, 1, CodecsSavePageMode.Append);
}
}
// Use the output Memory Stream containing the final portion here before final stream is freed and closed
_outputStream.Dispose();
_outputStream.Close();
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and splits the input sample document according to the barcode detection results.
This tutorial showed how to use the BarcodeData
class with the BarcodeSymbology
enumeration.