πŸ“š ImagingTest.cs Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
    1. In-Memory Database Setup
    2. File Discovery & Handling
    3. PDF Splitting & Manipulation
    4. OCR Processing
    5. Metadata Enrichment & Batch Insert
  3. Dependencies & Technologies
  4. Class & Method Breakdown
  5. Workflow Step-by-Step
  6. UML Class Diagram
  7. Sample Code Snippet
  8. Usage & Running the Test
  9. 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

  1. Setup In-Memory Database

    • Instantiate DbContextOptions<IrbyContext> with UseInMemoryDatabase
    • Call Database.EnsureCreated()
  2. File Discovery

    • Define an uploads path (temporary folder or local test directory)
    • Retrieve all PDF file paths using Directory.GetFiles(...)
  3. Document Loop

    • For each discovered PDF:
      a. Assign a new ReferenceId & BatchId
      b. Read entire file into a FormFile & byte[] (in-memory)
      c. Load PDF via IronPdf.PdfDocument.FromFile(...)
  4. Page Splitting

    • For each page in pdfDoc.PageCount:
      • Copy page via pdfDoc.CopyPage(idx)
      • Save single-page PDF to disk (optional temp file)
  5. OCR Processing

    • Setup IronTesseract & OcrInput
    • input.AddPdf(tempFilePath) + Deskew()
    • var ocrOutput = ocr.Read(input)
  6. Populate Domain Model

    • TempDocumentData fields:
      • ImageContents (byte[])
      • ImageOCRContents (searchable PDF bytes)
      • ImageOCRText (string)
      • Pagination: Page, TotalPages
      • Metadata: FileName, ReferenceId, BatchId, DocumentUploadedBy, DocumentUploadedDate, etc.
  7. Batch Insert

    • Accumulate up to 1000 TempDocumentData objects
    • _context.TempDocumentData.AddRange(documents)
    • _context.SaveChanges()
    • Reset batch

πŸ“ 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

  1. Prerequisites

    • .NET 6.0 SDK or later
    • Access to NuGet packages: xUnit, EF Core InMemory, IronPdf, IronOcr, iTextSharp
  2. Restore Packages

    dotnet restore
    
  3. Run Unit Tests

    dotnet test --filter "ImagingTest"
    
  4. Inspect Results

    • The test will execute an end-to-end import pipeline against an in-memory DB
    • Verify the count of TempDocumentData entries 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!