These examples are written for modern browsers, which support ES6 modules. ES5 version of our library also exists if needed.
Let's assume you have this structure:
demo/index.js - the place where you want to put examples
demo/index.html
src/TWTwainJS.js
src/TWTwainJSCapabilities.js
in index.html
in index.js at the top:
TWTwainJS instance has its name as identification, company name, and key for licensing.
connectToClient method creates a socket connection by asking the local TWTwain client to provide an available port in the range of fromPort-toPort.
Every change of socket connection status triggers the event TWEvent.SOCKET_CONNECTION_STATUS_CHANGED.
The most important one is information when the status is connected because, at that moment, it is possible to execute Twain commands (like getAvailableDevices or scan) on the local Twain client.
In all the following examples, we will assume that the twTwainJS instance is created and has successfully connected to the client.
To perform a scan, retrieving device info through the getDeviceInfo call isn't necessary to perform. Still, it is helpful if, before scanning, we want to know the device's current settings and its supported capabilities.
Returned deviceInfo result has the following properties:
currentSettings
supportedCapabilities
deviceName
Current settings are sent after the call of getDeviceInfo, and in most use cases, it is enough to use the value of the setting present at the moment of that call.
But it is also possible to ask what are current settings in separate calls and just for specific settings.
That could be useful if you want to be sure that the current settings retrieved are the up to date or if you want to get current settings for the device different from the last call of getDeviceInfo.
You can define one or many capability names at the same time.
Every capability available in CapName can retrieve its current value if the device supports that capability.
scan method uses ScanSettings to perform scanning.
ScanSettings has following properties:
scanFormatType
showUI
closeUIAfterAcquire
imageQuality %. It has effect only for PDF and JPG.
pdfProtection
tiffCompression
deviceName
Device name can be:
supportedCapabilities optional. if empty there will not be checked if the cap setting is valid.
ScanSettings has very important method setCap, for setting TWAIN capability value.
If supportedCapabilities is set, it will be used to check if the provided value can be set to that property.
You can use any of available standard TWAIN properties. CapName has a list of all available TWAIN property names.
Example of setCap
Scan operation triggers two types of event:
1. TWEvent.PAGE_SCANNED after every page scanned and has in event detail:
scanResult
deviceName
twTwainJSInstance
2. TWEvent.SCAN_FINISHED_SUCCESS after scan has finished completely and has in event detail:
scanResult
deviceName
twTwainJSInstance
scanFinishedStatus: TWScanFinishedStatus.SUCCESS, TWScanFinishedStatus.ERROR or TWScanFinishedStatus.CANCELLED
scanResult has following properties:
images: an array of base64 encoded scanned page images. For pdf and tiff it will be jpeg.
scanFormatType: jpeg, bmp, png, pdf or tiff
deviceName
tiffFile: the base64 encoded tiff file. Only available when a scan has finished and if scanFormatType is tiff.
pdfFile: the base64 encoded pdf file. when scan has finished and if scanFormatType is pdf.
scanResult has the following methods:
pageCount()
getImageDataAsUint8Array(pageNumber): returns byte[] which can be used to save to file or to upload somewhere.
getImageDataSrc(pageNumber) returns the image data source URL for a selected page number. Useful for displaying in canvas.
Example 1: Scan as JPEG, use feeder, set page size as A4, and display every page in canvas
Display the single page in canvas:
if scanFormatType is pdf or tiff, jpeg is used for single page image, otherwise, single image format is the same as scanFormatType
Example 2: Scan as Tiff.
It will generate a multi-page tiff file compressed with LZW compression.
Example 3: Scan as PDF
Generated PDF contains scanned pages with JPG images.
This example shows how to protect PDF document, but that is optional.
Using the downloadFile method, you can download pages that have already been scanned even though the scan still needs to finish completely.
Setting a file name is mandatory, and based on the file name extension is, determined format type of the file.
downloadFile has two parameters:
downloadMode: TWDownloadMode.STANDARD or TWDownloadMode.LOCAL
scannedFileSettings
ScannedFileSettings has the following properties:
fileName: test.tiff, some.pdf, etc. Supported are: pdf, tiff, jpg, bmp, png.
pages: if empty it will return all pages.
isMultiPageFile: valid only for pdf and tiff. If true, it will return all pages in single file.
pdfProtection: optional pdfProtection settings for pdf file
imageQuality: in %, it has effect only for jpeg and pdf
tiffCompression: one of the available tiff compressions for tiff file
The returned result has the following properties:
fileContent: array of retrieved files (if only one page or multi-page file it will have only one retrieved file)
fileName: scannedFileSettings.fileName
isMultiPageFile: scannedFileSettings.isMultiPageFile
pages: scannedFileSettings.pages
warning: warning message if some page numbers weren't correct
downloadMode: scannedFileSettings.downloadMode
twTwainJSInstance: twTwainJSInstance, which has executed getFile
Example 1: Download pages 1-3 as a single multi-page PDF using standard browser download mode where the user can choose its download location.
We assume that you have already called the scan method and scanned 3 pages.
Example 2: Download pages 3, 2, 2 as 3 jpeg files using local download mode.
Local download mode means that files will be saved in the location defined in TWTwainClient settings.
Example 3: Download pages 1-2, 3, 1 as one single multi-page tiff file using LZW compression
getFile method retrieves scanned pages.
It accepts ScannedFileSettings as a parameter. The same parameter type is also used in downloadFile.
Result of getFile method call is an object with the following properties:
fileContent: array of retrieved files (if only one page or multi-page file, it will have only one retrieved file)
fileName: scannedFileSettings.fileName
isMultiPageFile scannedFileSettings.isMultiPageFile
pages: scannedFileSettings.pages
warning: warning message if some page numbers weren't correct
twTwainJSInstance: twTwainJSInstance, which has executed getFile
Also, TWEvent.FILE_RETRIEVED is triggered where event.detail has the same properties as the promise result of getFile.
Example: Get multiple jpeg documents with pages 1-3.
We assume you have already called the scan method and scanned 3 pages.
Errors could be caught in two ways
1. directly in .catch from the promise call
2. by catching one of the available error events: TWEvent.SCAN_FINISHED_ERROR for scanning error or TWEvent.TW_ERROR for every other possible error