Table of Contents

Method ConvertAsync

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

ConvertAsync(string, OcrEngine, string, PdfGenerationOptions, CancellationToken)

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

public static Task ConvertAsync(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.

Returns

Task

A task representing the asynchronous operation.

Examples

Example: Asynchronously create a searchable PDF with LM-Kit OCR.

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

var ocr = new LMKitOcr();

await ImageToSearchablePdf.ConvertAsync( "invoice_scan.png", ocr, "invoice_searchable.pdf");

Exceptions

ArgumentNullException

imagePath, ocrEngine, or outputPath is null.

FileNotFoundException

The file specified by imagePath does not exist.

ConvertAsync(string, OcrResult, string, PdfGenerationOptions)

Asynchronous version of Convert(string, OcrResult, string, PdfGenerationOptions). Converts an image file to a searchable PDF using a precomputed OcrResult.

public static Task ConvertAsync(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.

Returns

Task

A task representing the asynchronous operation.

Examples

Example: Asynchronously create a searchable PDF from a cached OCR result.

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

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

await ImageToSearchablePdf.ConvertAsync("form.png", result, "form_searchable.pdf");

Exceptions

ArgumentNullException

imagePath, ocrResult, or outputPath is null.

FileNotFoundException

The file specified by imagePath does not exist.

ConvertAsync(string, PageElement, string, PdfGenerationOptions)

Asynchronous version of Convert(string, PageElement, string, PdfGenerationOptions). Converts an image file to a searchable PDF using a PageElement.

public static Task ConvertAsync(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.

Returns

Task

A task representing the asynchronous operation.

Examples

Example: Asynchronously convert with a PageElement.

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

PageElement layout = GetPageLayout("page.png"); await ImageToSearchablePdf.ConvertAsync("page.png", layout, "page.pdf");

Exceptions

ArgumentNullException

imagePath, pageElement, or outputPath is null.

FileNotFoundException

The file specified by imagePath does not exist.

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

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

public static Task ConvertAsync(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.

Returns

Task

A task representing the asynchronous operation.

Examples

Example: Binarize an image, then create a compact searchable PDF.

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

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

var options = new PdfGenerationOptions { Version = PdfGenerationOptions.PdfVersion.PdfA1b };

await ImageToSearchablePdf.ConvertAsync(image, ocr, "clean_output.pdf", options);

Exceptions

ArgumentNullException

imageBuffer, ocrEngine, or outputPath is null.

ConvertAsync(ImageBuffer, OcrResult, string, PdfGenerationOptions)

Asynchronous version of Convert(ImageBuffer, OcrResult, string, PdfGenerationOptions). Converts an in-memory ImageBuffer to a searchable PDF using a precomputed OcrResult.

public static Task ConvertAsync(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.

Returns

Task

A task representing the asynchronous operation.

Examples

Example: Asynchronously create a searchable PDF from an ImageBuffer and OcrResult.

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

var image = ImageBuffer.Load("scan.png"); var ocr = new LMKitOcr(); OcrResult result = await ocr.RunAsync(new OcrParameters(image));

await ImageToSearchablePdf.ConvertAsync(image, result, "scan.pdf");

Exceptions

ArgumentNullException

imageBuffer, ocrResult, or outputPath is null.

ConvertAsync(ImageBuffer, PageElement, string, PdfGenerationOptions)

Asynchronous version of Convert(ImageBuffer, PageElement, string, PdfGenerationOptions). Converts an in-memory ImageBuffer to a searchable PDF using a PageElement.

public static Task ConvertAsync(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.

Returns

Task

A task representing the asynchronous operation.

Examples

Example: Asynchronously create a searchable PDF from an ImageBuffer and PageElement.

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

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

await ImageToSearchablePdf.ConvertAsync(image, layout, "chart.pdf");

Exceptions

ArgumentNullException

imageBuffer, pageElement, or outputPath is null.

Share