π ImagingTest.cs Documentation
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Class & Method Breakdown
- Workflow Step-by-Step
- UML Class Diagram
- Sample Code Snippet
- Usage & Running the Test
- Conclusion
π Overview & Purpose
The ImagingTest class is part of the IrbyImaging.UnitTesting assembly. It is designed to demonstrate and validate a bulk import pipeline that:
- Reads PDF documents from a filesystem folder
- Splits multi-page PDFs into individual pages
- Runs OCR (Optical Character Recognition) on each page
- Wraps page images, text, and metadata into a domain model (
TempDocumentData) - Performs a batch insert into an in-memory database (
IrbyContext)
Although the test code is currently commented out, it provides a clear blueprint for an automated imaging ingestion and OCR workflow.
π Key Functional Areas
| Area | Description | Related Code / Library |
|---|---|---|
| In-Memory Database Setup | Creates a throwaway IrbyContext for isolated testing |
Microsoft.EntityFrameworkCore |
| File Discovery & Handling | Scans a configured folder for PDF files | System.IO, Microsoft.AspNetCore.Http |
| PDF Splitting & Manipulation | Splits multi-page PDF into single-page PDFs | IronPdf, iTextSharp.text.pdf |
| OCR Processing | Converts each PDF page into searchable PDF bytes and extracts text | IronOcr |
| Metadata Enrichment & Batch Insert | Populates TempDocumentData entities and performs batch DB inserts |
IrbyImaging.DomainClasses |
π¦ Dependencies & Technologies
| Package / Namespace | Purpose |
|---|---|
xUnit (Xunit) |
Unit testing framework |
EF Core (Microsoft.EntityFrameworkCore) |
In-memory database for testing |
| IronPdf | PDF loading, splitting, saving |
| iTextSharp | Low-level PDF page manipulation |
| IronOcr | OCR engine to extract searchable text & PDF bytes |
ASP.NET Core (Microsoft.AspNetCore.*) |
IFormFile & file abstractions |
| System.IO | File and stream operations |
| System.Text.RegularExpressions | Fine-grained text processing (future extension) |
ποΈ Class & Method Breakdown
namespace IrbyImaging.UnitTesting
{
public class ImagingTest
{
// COMMENTED OUT:
// private IrbyContext GetDbContext() { ... }
// [Fact]
// public void BulkImportIntoDB() { ... }
}
}
1. ImagingTest
- Assembly: IrbyImaging.UnitTesting
- Responsibility: Houses unit test(s) for imaging ingestion.
- Attributes: Leverages
[Fact]from xUnit to designate test methods.
2. GetDbContext() (commented)
Creates an IrbyContext backed by an In-Memory EF Core provider.
- Returns:
IrbyContext - Use Case: Isolated unit tests without hitting a real database.
3. BulkImportIntoDB() (commented)
Simulates a full import pipeline.
- Workflows:
- Scanning directories
- Wrapping files as
IFormFile - Splitting PDFs one page at a time
- Running OCR and extracting text/pdf-bytes
- Enriching domain object (
TempDocumentData) - Inserting in batches of 1000
π Workflow Step-by-Step
-
Setup In-Memory Database
- Instantiate
DbContextOptions<IrbyContext>withUseInMemoryDatabase - Call
Database.EnsureCreated()
- Instantiate
-
File Discovery
- Define an
uploadspath (temporary folder or local test directory) - Retrieve all PDF file paths using
Directory.GetFiles(...)
- Define an
-
Document Loop
- For each discovered PDF:
a. Assign a newReferenceId&BatchId
b. Read entire file into aFormFile&byte[](in-memory)
c. Load PDF viaIronPdf.PdfDocument.FromFile(...)
- For each discovered PDF:
-
Page Splitting
- For each page in
pdfDoc.PageCount:- Copy page via
pdfDoc.CopyPage(idx) - Save single-page PDF to disk (optional temp file)
- Copy page via
- For each page in
-
OCR Processing
- Setup
IronTesseract&OcrInput input.AddPdf(tempFilePath)+Deskew()var ocrOutput = ocr.Read(input)
- Setup
-
Populate Domain Model
TempDocumentDatafields:ImageContents(byte[])ImageOCRContents(searchable PDF bytes)ImageOCRText(string)- Pagination:
Page,TotalPages - Metadata:
FileName,ReferenceId,BatchId,DocumentUploadedBy,DocumentUploadedDate, etc.
-
Batch Insert
- Accumulate up to 1000
TempDocumentDataobjects _context.TempDocumentData.AddRange(documents)_context.SaveChanges()- Reset batch
- Accumulate up to 1000
π UML Class Diagram
classDiagram
class ImagingTest {
+GetDbContext() : IrbyContext
+BulkImportIntoDB()
}
class IrbyContext {
+DbSet<TempDocumentData> TempDocumentData
}
class TempDocumentData {
+Guid TempDocumentDataId
+Guid ReferenceId
+Guid BatchId
+string FileName
+int Page
+int TotalPages
+byte[] ImageContents
+byte[] ImageOCRContents
+string ImageOCRText
+string ContentType
+DateTime DocumentUploadedDate
+string DocumentUploadedBy
}
ImagingTest --> IrbyContext : uses
ImagingTest --> TempDocumentData : populates
π Sample Code Snippet
// Initialize In-Memory Database
var options = new DbContextOptionsBuilder<IrbyContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
var _context = new IrbyContext(options);
_context.Database.EnsureCreated();
// OCR engine
var ocr = new IronTesseract();
string uploads = Path.Combine(Path.GetTempPath(), "irbyuploads");
var docs = Directory.GetFiles(uploads);
foreach (var filepath in docs)
{
var batchId = Guid.NewGuid();
var refId = Guid.NewGuid();
using var pdfDoc = IronPdf.PdfDocument.FromFile(filepath);
for (int idx = 0; idx < pdfDoc.PageCount; idx++)
{
var tempData = new TempDocumentData
{
TempDocumentDataId = Guid.NewGuid(),
ReferenceId = refId,
BatchId = batchId,
DocumentUploadedDate = DateTime.Now,
DocumentUploadedBy = "System",
FileName = Path.GetFileName(filepath),
Page = idx + 1,
TotalPages = pdfDoc.PageCount
};
// Extract page
IronPdf.PdfDocument pageDoc = pdfDoc.CopyPage(idx);
using var memStream = new MemoryStream();
pageDoc.SaveAs(memStream);
tempData.ImageContents = memStream.ToArray();
// Perform OCR
using var input = new OcrInput();
input.AddPdf(memStream);
var result = ocr.Read(input);
tempData.ImageOCRText = result.Text;
tempData.ImageOCRContents = result.SaveAsSearchablePdfBytes();
tempData.ContentType = "application/pdf";
_context.TempDocumentData.Add(tempData);
}
}
_context.SaveChanges();
π Usage & Running the Test
-
Prerequisites
- .NET 6.0 SDK or later
- Access to NuGet packages: xUnit, EF Core InMemory, IronPdf, IronOcr, iTextSharp
-
Restore Packages
dotnet restore -
Run Unit Tests
dotnet test --filter "ImagingTest" -
Inspect Results
- The test will execute an end-to-end import pipeline against an in-memory DB
- Verify the count of
TempDocumentDataentries in context
π Conclusion
The ImagingTest.cs file is a comprehensive unit test prototype for a bulk document ingestion workflow. By combining PDF splitting, OCR and metadata enrichment, this test lays the groundwork for:
- Large-scale PDF imports
- Automated text extraction
- Searchable PDF archival
With minor adjustmentsβsuch as parameterizing paths, enabling/disabling temporary file writes, or integrating with a live SQL databaseβthis test can quickly evolve into a production-ready verification suite for the Irby Imaging platform.
Stay π Imaging and OCR-enabled!