Skip to main content

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.

.NET MAUI PDF Creation

.NET MAUI Text Detection

.NET MAUI Data Extraction

Supported file types

File typeDocumentDocumentDataReader
PDFyes
BMP, JPG, JPEG, PNG, TIFF, HEICyesyes

Which class to use depends on what you want to do with the file:

  • Document.LoadFile loads image files only. Use it if you just want to generate a non-searchable PDF or images from them.
  • DocumentDataReader.LoadFile loads images and PDF files. Use it for PDF files, and whenever you need text recognition or data extraction.

Initialization

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
}
caution

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;
}
tip

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;
}
info

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.