Asynchronous Methods
Every operation that touches the file system or runs text recognition comes in both a synchronous and an asynchronous flavour. The …Async variant does the work on a background thread, which keeps your UI responsive.
We recommend using the asynchronous variants, since a synchronous call occupies the thread it is called on. Calling them from your UI thread keeps your app responsive at all times.
| Synchronous | Asynchronous |
|---|---|
Document.LoadFile | Document.LoadFileAsync |
Document.WritePDF | Document.WritePDFAsync |
Document.WriteImage | Document.WriteImageAsync |
Document.GetImageBytes | Document.GetImageBytesAsync |
DocumentDataReader.LoadFile | DocumentDataReader.LoadFileAsync |
DocumentDataReader.GetText | DocumentDataReader.GetTextAsync |
DocumentDataReader.Analyze | DocumentDataReader.AnalyzeAsync |
DocumentDataReader.SetAnalyzeConfiguration | DocumentDataReader.SetAnalyzeConfigurationAsync |
DocutainSDK.DeleteTempFiles | DocutainSDK.DeleteTempFilesAsync |
Both variants take the same parameters, return the same values and report errors the same way, see Error Handling.
using Docutain.SDK.MAUI;
//...
//scan or import a file
//...
var destinationPath = Path.Combine(FileSystem.CacheDirectory, "TestPDF.pdf");
//synchronous, runs on the calling thread
var pdfFile = Document.WritePDF(destinationPath);
//asynchronous, runs on a background thread
var pdfFileAsync = await Document.WritePDFAsync(destinationPath);
UI.ScanDocument and UI.StartPhotoPayment are asynchronous by nature, since they wait for the user to finish. They have no synchronous counterpart.
Argument validation
The …Async variants validate their arguments and the initialization state before the background work is started. That means an ArgumentException or a DocutainSdkNotInitializedException is thrown by the call itself and not raised through the returned Task, so a try/catch around the await catches both cases.