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
imagePathstringPath to the source image file (PNG, JPEG, BMP, TIFF, etc.).
ocrEngineOcrEngineThe OCR engine to use for text recognition.
outputPathstringPath where the output PDF file will be written.
optionsPdfGenerationOptionsOptional PDF generation settings (image encoding, PDF version, PDF/A conformance). When
null, default options are used.cancellationTokenCancellationTokenToken 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, oroutputPathisnull.- FileNotFoundException
The file specified by
imagePathdoes 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
imagePathstringPath to the source image file.
ocrResultOcrResultThe OCR result containing recognized text elements and their positions.
outputPathstringPath where the output PDF file will be written.
optionsPdfGenerationOptionsOptional 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, oroutputPathisnull.- FileNotFoundException
The file specified by
imagePathdoes 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
imagePathstringPath to the source image file.
pageElementPageElementThe page layout containing text elements and their bounding boxes.
outputPathstringPath where the output PDF file will be written.
optionsPdfGenerationOptionsOptional 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, oroutputPathisnull.- FileNotFoundException
The file specified by
imagePathdoes 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
imageBufferImageBufferThe source image in memory.
ocrEngineOcrEngineThe OCR engine to use for text recognition.
outputPathstringPath where the output PDF file will be written.
optionsPdfGenerationOptionsOptional PDF generation settings. When
null, default options are used.cancellationTokenCancellationTokenToken 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, oroutputPathisnull.
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
imageBufferImageBufferThe source image in memory.
ocrResultOcrResultThe OCR result containing recognized text elements and their positions.
outputPathstringPath where the output PDF file will be written.
optionsPdfGenerationOptionsOptional 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, oroutputPathisnull.
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
imageBufferImageBufferThe source image in memory.
pageElementPageElementThe page layout containing text elements and their bounding boxes.
outputPathstringPath where the output PDF file will be written.
optionsPdfGenerationOptionsOptional 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, oroutputPathisnull.