Table of Contents

๐Ÿ‘‰ Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/document-intelligence/document-splitting/barcode_document_splitting

Barcode Document Splitting for C# .NET Applications (PDF, TIFF and Images)


๐ŸŽฏ Purpose of the Demo

The Barcode Document Splitting demo shows how to use the LM-Kit.NET SDK to split a scanned batch into separate documents by the barcodes printed on its pages, and to write one file per document named after the barcode value.

The split is deterministic: barcodes are read by the native barcode reader built into LM-Kit.NET. No model is downloaded or loaded, so the demo has no model selection menu and needs no GPU.


๐Ÿ‘ฅ Who Should Use This Demo

  • Mailroom and capture teams: batch-scan hundreds of documents behind QR or Code 128 separator sheets and file each one automatically.
  • Accounts payable: split invoice batches by invoice number, purchase order or supplier code.
  • Healthcare and insurance back offices: group pages by patient, claim or policy barcode, even when they arrive out of order.
  • Developers of capture pipelines: replace a manual "split by patch sheet" step with a few lines of C#.

๐Ÿš€ What Problem It Solves

Production scanners turn a stack of paper into one long PDF or TIFF. Operators already mark document boundaries with barcode sheets or stamped barcodes; what is missing is the step that reads them and cuts the batch.

This demo automates that step:

  • every page is scanned for barcodes
  • matching barcodes become document boundaries according to the chosen mode
  • the blank back of each separator sheet (duplex scans) is removed
  • each document is written to its own PDF, named after its barcode value

๐Ÿ’ป Demo Application Overview

The console app:

  • asks for a PDF, TIFF or image batch, or generates a sample batch when you press Enter (a multi-page TIFF with QR separator sheets encoded by LMKit.Graphics.QrCode, each followed by a blank back)
  • asks what a barcode means (SeparatorPage, FirstPageOfDocument, LastPageOfDocument, ValueChange, GroupByValue)
  • asks for an optional value pattern, a regular expression whose value group becomes the document's value
  • calls BarcodeSplitting.SplitToFiles with FileNameTemplate = "{value}_{index:000}" and RemoveBlankPageAfterSeparator = true
  • prints every page with its role and barcodes, every document with its pages and file, the separator, discarded and failed pages, and the elapsed time

โœจ Key Features

  • Five splitting modes: separator sheets, first or last page markers, value changes, grouping by value.
  • Value extraction: ValuePattern keeps only the part of the barcode text you need (INV from INV-1001).
  • Duplex aware: RemoveBlankPageAfterSeparator drops the blank back of a removed separator sheet.
  • Named outputs: {value}, {index:000}, {source}, {start}, {end}, {pages} and {format} tokens.
  • Lossless PDF output: a PDF source keeps its pages as they are; pages are extracted, never rasterized.
  • Complete audit trail: every page reports its BarcodePageRole and the barcodes read on it.

๐Ÿ’ป Minimal Integration Snippet

using LMKit.Extraction.Barcodes;

var options = new BarcodeSplittingOptions
{
    Mode = BarcodeSplitMode.SeparatorPage,
    RemoveBlankPageAfterSeparator = true,
    FileNameTemplate = "{value}_{index:000}"
};

var splitter = new BarcodeSplitting(options);
BarcodeSplittingResult result = splitter.SplitToFiles("scanned_batch.pdf", "output");

foreach (BarcodeSegment segment in result.Segments)
{
    Console.WriteLine($"{segment.Label}: {segment.PageCount} page(s) -> {segment.OutputPath}");
}

Example Output

Sample batch, SeparatorPage mode:

---------- Documents ----------
    1. INV-1001             pages 3, 4  -> INV-1001_001.pdf
    2. PO-2001              pages 7  -> PO-2001_002.pdf
    3. INV-1002             pages 10, 11, 12  -> INV-1002_003.pdf
    4. INV-1001             pages 15  -> INV-1001_004.pdf

---------- Summary ----------
  source           : scanned_batch.tif
  pages            : 15
  documents        : 4
  separator pages  : 1, 5, 8, 13
  discarded pages  : 1, 2, 5, 6, 8, 9, 13, 14
  failed pages     : none

Same batch, GroupByValue mode with the value pattern ^(?<value>[A-Z]+)-: INV_001.pdf (12 pages) and PO_002.pdf (3 pages).


๐Ÿ—๏ธ Architecture

 scanned batch (PDF / TIFF / image)
              |
              v
 +---------------------------+
 | BarcodeSplitting          |   page scan: barcodes + blank test per page
 |   Match (which barcodes)  |
 |   Mode  (what they mean)  |
 |   ValuePattern (value)    |
 +---------------------------+
              |
              v
 BarcodeSplittingResult
   Pages    : role + barcodes of every page
   Segments : documents (pages, value, trigger barcode)
              |
              v
 SplitToFiles -> {value}_{index:000}.pdf, one file per document

โš™๏ธ Getting Started

๐Ÿ“‹ Prerequisites

  • .NET 8.0 or later
  • No GPU, no VRAM and no model download: the demo runs entirely on the CPU

๐Ÿ“ฅ Download the Project

โ–ถ๏ธ Running the Application

  1. Clone the repository:
git clone https://github.com/LM-Kit/lm-kit-net-samples
  1. Navigate to the project directory:
cd lm-kit-net-samples/console_net/document-intelligence/document-splitting/barcode_document_splitting
  1. Build and run the application:
dotnet build
dotnet run
  1. Follow the prompts: press Enter for the sample batch, pick a mode, optionally enter a value pattern. The documents are written to <batch name>_split next to the batch.

๐Ÿ”ง Troubleshooting

  • One document holds the whole batch: no barcode matched. Check that the barcodes are printed large enough for the scan resolution, and that Match does not filter them out.
  • Wrong pages become separators: a page carries an unrelated barcode (a shipping label, a product code). Restrict Match with Formats, Values, Pattern or SearchArea.
  • Pages listed under failed pages: those pages could not be read; they stay content and the error is reported on BarcodePage.Error.
  • Files named name (2).pdf: the output folder already held a file of that name; FileConflict defaults to MakeUnique.

๐Ÿš€ Extend the Demo

  • Keep the separator sheet with SeparatorHandling = SeparatorPageHandling.KeepAsFirstPage.
  • Write multi-page TIFF outputs with OutputFormat = BarcodeSplitOutputFormat.Tiff.
  • Stream documents to blob storage with SplitToStreams instead of writing files.
  • Cancel or log per page with the PageScanned and PartWritten events.
  • Feed each document to TextExtraction or a classifier to route it downstream.

๐Ÿ“š Additional Resources

Share