Image Export
In addition to exporting scanned documents as PDF, you can also retrieve the pages of the currently scanned document as image files (JPG).
Initialization
- Follow the Getting started guide
- Initialize the Docutain .NET MAUI SDK as described here
Export scanned pages as image files
Write images to local files
In order to write the currently scanned pages to local JPG files, you can use the Document.WriteImage method. Pass the page you want to export as JPG and the target path where to save it. If you want to export all pages as JPG, you can get the number of pages via Document.PageCount and loop through all pages like in the follwing example:
using Docutain.SDK.MAUI;
//...
//scan a document
//...
int pages = Document.PageCount;
for (int p = 1; p <= pages; p++)
{
string targetFilePath = Path.Combine(path, $"Image_{p}.jpg");
string? imageFile = await Document.WriteImageAsync(p, targetFilePath);
if (imageFile == null)
{
//error occured
var error = DocutainSDK.LastError;
}
}
Pages start at 1. Document.WriteImage and Document.GetImageBytes throw an ArgumentOutOfRangeException for a page number below 1.
Get images as byte[]
Getting the pages as JPG byte array improves performance as it does not include any Disk I/O. To do so, you can call Document.GetImageBytes, pass it the page number of the page to be exported as JPG and an optional PageSourceType.
using Docutain.SDK.MAUI;
//...
//scan a document
//...
int pages = Document.PageCount;
for (int p = 1; p <= pages; p++)
{
byte[]? image = await Document.GetImageBytesAsync(p);
}
Both methods are also available synchronously. See Asynchronous Methods for more details.
A return value of null means the image could not be generated, DocutainSDK.LastError states the reason. If the SDK has not been initialized successfully, a DocutainSdkNotInitializedException is thrown instead. See Error Handling for more details.
PageSourceType
When getting the images as byte[], you can define a PageSourceType.
//...
//scan a document
//...
int pages = Document.PageCount;
for (int p = 1; p <= pages; p++)
{
byte[]? image = await Document.GetImageBytesAsync(p, PageSourceType.CutFilter);
}
You have the following options:
| Value | Description |
|---|---|
CutFilter | The cut and filtered image, which is the one the user sees when finishing the scan process. This is the default value. |
CutOnly | The cut but unfiltered image. If you for example use the image for further processing in your own OCR pipeline which uses custom filter operations, this option might improve your OCR results as opposed to CutFilter. But this is no general rule and highly depends on your pipeline. |
Original | The uncut, unfiltered image as it was provided by the camera. |