๐ 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
valuegroup becomes the document's value - calls
BarcodeSplitting.SplitToFileswithFileNameTemplate = "{value}_{index:000}"andRemoveBlankPageAfterSeparator = 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:
ValuePatternkeeps only the part of the barcode text you need (INVfromINV-1001). - Duplex aware:
RemoveBlankPageAfterSeparatordrops 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
BarcodePageRoleand 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
- Clone the repository:
git clone https://github.com/LM-Kit/lm-kit-net-samples
- Navigate to the project directory:
cd lm-kit-net-samples/console_net/document-intelligence/document-splitting/barcode_document_splitting
- Build and run the application:
dotnet build
dotnet run
- Follow the prompts: press Enter for the sample batch, pick a mode, optionally enter a value pattern. The documents are written to
<batch name>_splitnext 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
Matchdoes not filter them out. - Wrong pages become separators: a page carries an unrelated barcode (a shipping label, a product code). Restrict
MatchwithFormats,Values,PatternorSearchArea. - 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;FileConflictdefaults toMakeUnique.
๐ 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
SplitToStreamsinstead of writing files. - Cancel or log per page with the
PageScannedandPartWrittenevents. - Feed each document to
TextExtractionor a classifier to route it downstream.
๐ Additional Resources
- How-To: Split Documents by Barcode: step-by-step guide to the barcode splitting API.
- Glossary: Barcode Document Separation: separator sheets, patch codes and barcode-driven splitting.
- Document Splitting Demo: content-based splitting with a vision model when the batch carries no barcodes.