Table of Contents

Method Convert

Namespace
LMKit.Document.Conversion
Assembly
LM-Kit.NET.dll

Convert(string, OcrEngine, string, PdfGenerationOptions, CancellationToken)

Converts an image file to a searchable PDF synchronously. Runs OCR on the image, then builds a PDF with the image layer and an invisible text overlay for search and selection.

public static void Convert(string imagePath, OcrEngine ocrEngine, string outputPath, PdfGenerationOptions options = null, CancellationToken cancellationToken = default)

Parameters

imagePath string

Path to the source image file (PNG, JPEG, BMP, TIFF, etc.).

ocrEngine OcrEngine

The OCR engine to use for text recognition.

outputPath string

Path where the output PDF file will be written.

options PdfGenerationOptions

Optional PDF generation settings (image encoding, PDF version, PDF/A conformance). When null, default options are used.

cancellationToken CancellationToken

Token to cancel the operation.

Examples

Example: Create a PDF/A-1b compliant searchable PDF.

using LMKit.Document.Conversion;
using LMKit.Document.Pdf;
using LMKit.Extraction.Ocr;

var ocr = new LMKitOcr(); var options = new PdfGenerationOptions { Version = PdfGenerationOptions.PdfVersion.PdfA1b };

ImageToSearchablePdf.Convert( "contract_scan.png", ocr, "contract_archive.pdf", options);

Exceptions

ArgumentNullException

imagePath, ocrEngine, or outputPath is null.

FileNotFoundException

The file specified by imagePath does not exist.

Convert(string, OcrResult, string, PdfGenerationOptions)

Converts an image file to a searchable PDF using a precomputed OcrResult. Useful when OCR has already been performed and results are cached.

public static void Convert(string imagePath, OcrResult ocrResult, string outputPath, PdfGenerationOptions options = null)

Parameters

imagePath string

Path to the source image file.

ocrResult OcrResult

The OCR result containing recognized text elements and their positions.

outputPath string

Path where the output PDF file will be written.

options PdfGenerationOptions

Optional PDF generation settings. When null, default options are used.

Examples

Example: Run OCR once, then create multiple output PDFs.

using LMKit.Document.Conversion;
using LMKit.Extraction.Ocr;
using LMKit.Extraction.Ocr;

var ocr = new LMKitOcr(); OcrResult result = await ocr.RunAsync( new Data.Attachment("receipt.png"), pageIndex: 0);

// Create a standard PDF ImageToSearchablePdf.Convert("receipt.png", result, "receipt.pdf");

// Create a PDF/A-1b archival copy from the same OCR result var archiveOptions = new PdfGenerationOptions { Version = PdfGenerationOptions.PdfVersion.PdfA1b }; ImageToSearchablePdf.Convert("receipt.png", result, "receipt_archive.pdf", archiveOptions);

Exceptions

ArgumentNullException

imagePath, ocrResult, or outputPath is null.

FileNotFoundException

The file specified by imagePath does not exist.

Convert(string, PageElement, string, PdfGenerationOptions)

Converts an image file to a searchable PDF using a PageElement that contains positioned text elements. This overload is useful when text layout comes from a source other than OcrResult.

public static void Convert(string imagePath, PageElement pageElement, string outputPath, PdfGenerationOptions options = null)

Parameters

imagePath string

Path to the source image file.

pageElement PageElement

The page layout containing text elements and their bounding boxes.

outputPath string

Path where the output PDF file will be written.

options PdfGenerationOptions

Optional PDF generation settings. When null, default options are used.

Examples

Example: Build a searchable PDF from a custom PageElement layout.

using LMKit.Document.Conversion;
using LMKit.Document.Layout;

// Assume pageElement was obtained from a document layout analysis pipeline. PageElement pageElement = GetPageLayout("document.png");

ImageToSearchablePdf.Convert("document.png", pageElement, "document_searchable.pdf");

Exceptions

ArgumentNullException

imagePath, pageElement, or outputPath is null.

FileNotFoundException

The file specified by imagePath does not exist.

Convert(ImageBuffer, OcrEngine, string, PdfGenerationOptions, CancellationToken)

Converts an in-memory ImageBuffer to a searchable PDF synchronously. Runs OCR on the image buffer, then builds the PDF.

public static void Convert(ImageBuffer imageBuffer, OcrEngine ocrEngine, string outputPath, PdfGenerationOptions options = null, CancellationToken cancellationToken = default)

Parameters

imageBuffer ImageBuffer

The source image in memory.

ocrEngine OcrEngine

The OCR engine to use for text recognition.

outputPath string

Path where the output PDF file will be written.

options PdfGenerationOptions

Optional PDF generation settings. When null, default options are used.

cancellationToken CancellationToken

Token to cancel the operation.

Examples

Example: Convert a binarized image to a searchable PDF with CCITT compression.

using LMKit.Document.Conversion;
using LMKit.Extraction.Ocr;
using LMKit.Media.Image;

var ocr = new LMKitOcr(); var image = ImageBuffer.Load("scan.png"); image = image.SmartBinarize();

// Binary images default to CCITT Group 4 compression (optimal for scanned docs). ImageToSearchablePdf.Convert(image, ocr, "scan_searchable.pdf");

Exceptions

ArgumentNullException

imageBuffer, ocrEngine, or outputPath is null.

Convert(ImageBuffer, OcrResult, string, PdfGenerationOptions)

Converts an in-memory ImageBuffer to a searchable PDF using a precomputed OcrResult.

public static void Convert(ImageBuffer imageBuffer, OcrResult ocrResult, string outputPath, PdfGenerationOptions options = null)

Parameters

imageBuffer ImageBuffer

The source image in memory.

ocrResult OcrResult

The OCR result containing recognized text elements and their positions.

outputPath string

Path where the output PDF file will be written.

options PdfGenerationOptions

Optional PDF generation settings. When null, default options are used.

Examples

Example: Preprocess an image, run OCR, then generate the PDF separately.

using LMKit.Document.Conversion;
using LMKit.Document.Pdf;
using LMKit.Extraction.Ocr;
using LMKit.Extraction.Ocr;
using LMKit.Media.Image;

// Load and preprocess var image = ImageBuffer.Load("photo_of_document.jpg"); image = image.SmartBinarize();

// Run OCR var ocr = new LMKitOcr(); OcrResult result = await ocr.RunAsync(new OcrParameters(image));

// Generate PDF with lossless compression var options = new PdfGenerationOptions { Rgb24Encoding = PdfGenerationOptions.ImageEncoding.Deflate };

ImageToSearchablePdf.Convert(image, result, "document.pdf", options);

Exceptions

ArgumentNullException

imageBuffer, ocrResult, or outputPath is null.

Convert(ImageBuffer, PageElement, string, PdfGenerationOptions)

Converts an in-memory ImageBuffer to a searchable PDF using a PageElement that contains positioned text elements.

public static void Convert(ImageBuffer imageBuffer, PageElement pageElement, string outputPath, PdfGenerationOptions options = null)

Parameters

imageBuffer ImageBuffer

The source image in memory.

pageElement PageElement

The page layout containing text elements and their bounding boxes.

outputPath string

Path where the output PDF file will be written.

options PdfGenerationOptions

Optional PDF generation settings. When null, default options are used.

Examples

Example: Create a searchable PDF from a custom page layout.

using LMKit.Document.Conversion;
using LMKit.Document.Layout;
using LMKit.Media.Image;

var image = ImageBuffer.Load("page.png"); PageElement layout = AnalyzePageLayout(image);

ImageToSearchablePdf.Convert(image, layout, "page_searchable.pdf");

Exceptions

ArgumentNullException

imageBuffer, pageElement, or outputPath is null.

Share