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 React Native Scanner SDK.
Supported file types
- BMP
- JPG
- JPEG
- PNG
- TIFF
- HEIC
- WebP
Initialization
- Follow the Getting started guide
- Initialize the Docutain React Native SDK as described here
File Import
To import a file, use loadFile. Afterwards you can create a searchable PDF (includes text), or get the detected data or text of the imported file.
You can pass an absolute path or a file:// URL. On Android, content:// URIs are supported as well, e.g. if the user picked the file with a document picker.
The sample uses the react-native-fs package to get a writable directory.
import DocutainSDK, {PDFPageFormat} from '@docutain/react-native-docutain-sdk';
import RNFS from 'react-native-fs';
if (await DocutainSDK.loadFile(filePath)) {
//generate a searchable PDF from the imported file
const destinationPath: string = RNFS.DocumentDirectoryPath + '/TestPDF.pdf';
const pdfFile = await DocutainSDK.writePDF(destinationPath, true, PDFPageFormat.A4);
if (pdfFile) {
//do something with the generated pdf file
} else {
//error occured
}
//get the detected text
const text = await DocutainSDK.getText();
//get the detected data
const data = await DocutainSDK.analyze();
} else {
//error occured
const error: string = await DocutainSDK.getLastError();
}
If you load a file from a path, the file needs to be readable by your app. The Docutain SDK reads the file directly and does not request any permissions.
Password protected PDF files
If the PDF file is password protected, you can provide the password as second parameter.
If the file is encrypted and no password or the wrong password was provided, loadFile returns false on iOS, while the promise is rejected on Android. So make sure to handle both cases:
try {
if (await DocutainSDK.loadFile(filePath, 'password')) {
//file was loaded
} else {
//error occured, e.g. the password is wrong or missing (iOS)
const error: string = await DocutainSDK.getLastError();
}
} catch (error) {
//the password is wrong or missing (Android)
}
Load from memory
If the file is already available in memory, e.g. when it was received from a server, you can load it directly with loadFileBytes without writing a temporary file first. Pass the file content as Base64 encoded string and the file extension without a leading dot, so Docutain knows the file type.
if (await DocutainSDK.loadFileBytes(base64String, 'pdf')) {
//get the detected text
const text = await DocutainSDK.getText();
//get the detected data
const data = await DocutainSDK.analyze();
} else {
//error occured
const error: string = await DocutainSDK.getLastError();
}
If the PDF file is password protected, you can provide the password as third parameter. A wrong or missing password is reported the same way as described for loadFile.
const loaded = await DocutainSDK.loadFileBytes(base64String, 'pdf', 'password');
Add pages
To add further pages to the currently loaded document, use addPageFile with the path to a file or addPageBytes with a Base64 encoded string and its file extension.
if (await DocutainSDK.loadFile(filePath)) {
await DocutainSDK.addPageFile(secondFilePath);
await DocutainSDK.addPageBytes(base64Image, 'jpg');
}
Pages can only be added from images, not from PDFs.
Both methods return false if the data is invalid, the file type is not supported or the file doesn't exist. You can get the error message by calling DocutainSDK.getLastError().