Classify Documents in C# .NET

This guide explains how to recognize barcodes in a collection of documents and classify them accordingly. For example, this feature enables you to sort documents based on the type and value of their barcodes.

To classify documents based on the type and value of their barcodes, follow the steps outlined below. This procedure assumes that the source documents are all image files. The documents are classified based on the first scanned 1D barcode.

  1. Create a GdPictureImaging object.

  2. Define the source and the output folders. The source folder is where the original documents are. The output folder is where the documents are saved with a file name that includes information about the type and value of the barcode.

  3. Loop through the files in the source folder.

  4. Scan the 1D barcodes in each image using the Barcode1DReaderDoScan method.

  5. Save the document in the output folder with a file name that includes information about the type and value of the first 1D barcode in the document.

  6. Release unnecessary resources.

The example below recognizes the 1D barcodes in each document within the source folder and saves the document in the output folder with a file name that includes information about the type and value of the barcode:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Define the source and output folders.
string sourceFolder = @"C:\temp\source\";
string outputFolder = @"C:\temp\output\";
string[] allFiles = Directory.GetFiles(sourceFolder);
int imageID = 0;
// Loop through the files in the source folder.
foreach (string sourceFilePath in allFiles)
{
    imageID = gdpictureImaging.CreateGdPictureImageFromFile(sourceFilePath);
    // Scan the barcode.
    gdpictureImaging.Barcode1DReaderDoScan(imageID);
    // Save the document in the output folder with a file name that includes information about the type and value of the first barcode in the document.
    string outputFilepath = $"{outputFolder}{gdpictureImaging.Barcode1DReaderGetBarcodeType(1)}-{gdpictureImaging.Barcode1DReaderGetBarcodeValue(1)}.png";
    gdpictureImaging.SaveAsPNG(imageID, outputFilepath);
    // Release unnecessary resources.
    gdpictureImaging.ReleaseGdPictureImage(imageID);
}
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Define the source and output folders.
    Dim sourceFolder = "C:\temp\source\"
    Dim outputFolder = "C:\temp\output\"
    Dim allFiles = Directory.GetFiles(sourceFolder)
    Dim imageID = 0
    ' Loop through the files in the source folder.
    For Each sourceFilePath In allFiles
        imageID = gdpictureImaging.CreateGdPictureImageFromFile(sourceFilePath)
        ' Scan the barcode.
        gdpictureImaging.Barcode1DReaderDoScan(imageID)
        ' Save the document in the output folder with a file name that includes information about the type and value of the first barcode in the document.
        Dim outputFilepath = $"{outputFolder}{gdpictureImaging.Barcode1DReaderGetBarcodeType(1)}-{gdpictureImaging.Barcode1DReaderGetBarcodeValue(1)}.png"
        gdpictureImaging.SaveAsPNG(imageID, outputFilepath)
        ' Release unnecessary resources.
        gdpictureImaging.ReleaseGdPictureImage(imageID)
    Next
End Using
Used Methods and Properties

Related Topics