File Import
You can import images or PDF files into Docutain and do a bunch of things with them, same as after a successful document scan with Docutain's .NET MAUI Scanner SDK.
Supported file types
| File type | Document | DocumentDataReader |
|---|---|---|
| – | yes | |
| BMP, JPG, JPEG, PNG, TIFF, HEIC | yes | yes |
Which class to use depends on what you want to do with the file:
Document.LoadFileloads image files only. Use it if you just want to generate a non-searchable PDF or images from them.DocumentDataReader.LoadFileloads images and PDF files. Use it for PDF files, and whenever you need text recognition or data extraction.
Initialization
- Follow the Getting started guide
- Initialize the Docutain .NET MAUI SDK as described here
File Import
If your simple goal is to import image files and generate a non-searchable (includes no text) PDF file from it, use the Document class.
using Docutain.SDK.MAUI;
try
{
if(await Document.LoadFileAsync(filePath))
{
//generate a PDF from the imported file
var destinationPath = Path.Combine(FileSystem.CacheDirectory, "TestPDF.pdf");
var pdfFile = await Document.WritePDFAsync(destinationPath);
if(pdfFile != null)
{
//do something with the generated pdf file
}
else
{
//error occured
var error = DocutainSDK.LastError;
}
}
else
{
//error occured
var error = DocutainSDK.LastError;
}
}
catch (DocutainSdkNotInitializedException)
{
//the Docutain SDK has not been initialized successfully
}
Document.LoadFile does not load PDF files. To import a PDF, use DocumentDataReader.LoadFile as shown below.
If you want to import a PDF file, create a searchable PDF (includes text), or get the detected data or text of the imported file, use the DocumentDataReader class.
using Docutain.SDK.MAUI;
try
{
if(await DocumentDataReader.LoadFileAsync(filePath))
{
//generate a searchable PDF from the imported file
var destinationPath = Path.Combine(FileSystem.CacheDirectory, "TestPDF.pdf");
var pdfFile = await Document.WritePDFAsync(destinationPath);
//get the detected text
var text = await DocumentDataReader.GetTextAsync();
//get the detected data
var data = await DocumentDataReader.AnalyzeAsync();
}
else
{
//error occured
var error = DocutainSDK.LastError;
}
}
catch (DocutainSdkNotInitializedException)
{
//the Docutain SDK has not been initialized successfully
}
catch (DocutainSdkFileAccessException ex)
{
//the file is an encrypted PDF, see Encrypted PDF files below
var message = ex.Message;
}
Both LoadFile methods are also available synchronously. See Asynchronous Methods for more details.
Encrypted PDF files
If the file to import is an encrypted PDF, pass the password as second parameter. If the password is missing or wrong, a DocutainSdkFileAccessException is thrown.
using Docutain.SDK.MAUI;
try
{
if (await DocumentDataReader.LoadFileAsync(filePath, password))
{
//...
}
}
catch (DocutainSdkNotInitializedException)
{
//the Docutain SDK has not been initialized successfully
}
catch (DocutainSdkFileAccessException ex)
{
//the password was missing or wrong, or the file could not be accessed
var message = ex.Message;
}
Document.LoadFile takes a password parameter as well, but it is not evaluated — it only loads image files, and those are never encrypted. Encrypted PDF files are always opened via DocumentDataReader.LoadFile.
On Android, DocutainSdkFileAccessException is also raised for a file your app is not allowed to read, so check the message before telling the user their password was wrong.