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 Kotlin Multiplatform Scanner SDK.

Kotlin Multiplatform PDF Creation

Kotlin Multiplatform Text Recognition

Kotlin Multiplatform Data Extraction

Supported file types​

The native Docutain data reader supports PDF, BMP, JPG, JPEG, PNG, TIFF, HEIC and WebP files.

Use DocutainSdk.loadFile to import files for text recognition, data extraction and subsequent exports.

Initialization​

  • Follow the Getting started guide
  • Initialize the Docutain SDK for Kotlin Multiplatform as described here

File Import​

If you want to import a PDF file, create a searchable PDF, or get the detected data or text of the imported file, use DocutainSdk.loadFile.

import de.docutain.sdk.kmp.DocutainException
import de.docutain.sdk.kmp.DocutainSdk

viewModelScope.launch {
try {
DocutainSdk.loadFile(filePath)

// generate a PDF from the imported file
val pdfFile = DocutainSdk.writePDF()

// get the detected text
val text = DocutainSdk.getText()

// get the detected data
val data = DocutainSdk.analyze()
} catch (exception: DocutainException) {
// the file could not be loaded or processed
val message = exception.message
}
}

loadFile is a suspend function. See Coroutines and suspend functions for more details.

The supplied value can be an absolute file path or a file:// URL. The file must be readable by the app.

Encrypted PDF files​

If the PDF is encrypted, pass its password to loadFile. A missing or wrong password, or an unreadable file, causes DocutainException:

viewModelScope.launch {
try {
DocutainSdk.loadFile(filePath, password = password)
// Work with the imported PDF.
} catch (exception: DocutainException) {
// Check the error before telling the user the password was wrong.
val message = exception.message
}
}

Load from memory​

If the file is already available as a ByteArray, e.g. when it was received from a server, you can load it directly without writing a temporary file first. Provide the file extension without a leading dot, so Docutain knows the file type. Encrypted PDFs work the same way, just pass the password.

viewModelScope.launch {
try {
DocutainSdk.loadFile(imageBytes, fileExtension = "jpg")

// or for an encrypted PDF
DocutainSdk.loadFile(pdfBytes, fileExtension = "pdf", password = password)
} catch (exception: DocutainException) {
// the data could not be loaded
val message = exception.message
}
}

Add pages​

To add further pages to the currently loaded document, use addPage with a file path or file:// URL, or with a ByteArray and its file extension.

viewModelScope.launch {
try {
DocutainSdk.loadFile(filePath)
DocutainSdk.addPage(secondFilePath)
DocutainSdk.addPage(imageBytes, fileExtension = "jpg")
} catch (exception: DocutainException) {
// the page could not be added
val message = exception.message
}
}

loadFile and addPage throw DocutainException if the data is invalid, the file type is not supported or the file doesn't exist. See Error Handling for more details.