Split Document with Patch Codes - Console C#

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# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Windows Console C# Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET Framework (Console)
  • Apple: macOS

Required Knowledge

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 - Console C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License 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).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included With 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

How to properly add LEADTOOLS NuGet and local references is covered in the Add References and Set a License tutorial.

Add the Split Document Code

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.

C#
// Using block at the top 
using System; 
using System.IO; 
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.

C#
static void Main(string[] args) 
{ 
   try 
   { 
      string _inputFile = @"INPUT DOCUMENT FILE PATH"; 
 
      SetLicense(); 
 
      using (RasterCodecs _codecs = new RasterCodecs()) 
      { 
         CodecsImageInfo _info = _codecs.GetInformation(_inputFile, true); 
 
         for (int i = 1; i <= _info.TotalPages; i++) 
         { 
            using (RasterImage _image = _codecs.Load(_inputFile, i)) 
               SplitPatchDocument(_image, i); 
         } 
      } 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.ToString()); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

Add a new method called SplitPatchDocument(RasterImage image, int _pageNum) 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.

C#
public static void SplitPatchDocument(RasterImage image, int _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 here, save out the portion of the document appended so far 
   if (data.Length > 0) 
   { 
      using (RasterCodecs codecs = new RasterCodecs()) 
      { 
         string filename = string.Format("split_Page{0}.tif", _pageNum); 
         string _splitDir = Path.Combine(@"C:\Temp\", filename); 
         codecs.Save(image, _splitDir, RasterImageFormat.Tif, 0); 
      } 
   } 
   else 
   { 
      using (RasterCodecs codecs = new RasterCodecs()) 
         codecs.Save(image, @"C:\Temp\NonPatchCodeDocumentPages.tif", 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 the AcquirePage event handler of a TWAIN or WIA session. For more details, see the Acquire an Image From a TWAIN Source tutorial.

Run the Project

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.

Wrap-Up

This tutorial showed how to use the BarcodeData class with the BarcodeSymbology.PatchCode symbology.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.