Help / WLanguage / WLanguage functions / Standard functions / Windows management functions / TWAIN functions
  • Example procedure for scanning documents from the feeder and save them as PDF files
TwainState (Example)
Example procedure for scanning documents from the feeder and save them as PDF files
WINDEVReports and QueriesUser code (UMC) This example uses a WLanguage procedure to scan documents from the feeder and save them as PDF files.
// Select the driver
IF TwainSelectSource() = False THEN RETURN

// Information of the print window
iWindowCancel(True, "Scan in PDF format")
// Implement the parameters of PDF
iParameterPDF(iProtectionNone, "" , "" , iBookmarkNone + iMinimumQuality)
// Name of PDF
sNameScannedFile is string
sNameScannedFile = fDataDir() + ["\"] + "Scan_" + DateSys() + "_" + TimeSys() + ".PDF"

// Start generating the PDF (see the detailed procedure below)
nNbPages is int
nNbPages = TwainMultiPageToPDF(sNameScannedFile, 90, False, TwainBlackWhite, 1)
// At least one digitized page?
IF nNbPages = 0 THEN
// Error during the scan
Error("The digitization failed", ErrorInfo())
// Display the generated file
// Display an information message
Info(StringBuild("%1 pages have been digitized in %2 file.", ...
nNbPages, sNameScannedFile))
//------- With the following TwainMultiPageToPDF procedure:
// Summary: Saves the documents found in the driver of the Twain device
// in a PDF file
// Syntax:
//[ <Result> = ] TwainMultiPageToPDF (<sPDFName> [, <nQuality> is int
// [, <bUserInterface> [, <nImageType> [, <nNumberBitsPerPixel> [, <nImageResolution>
// [, <nImageContrast> [, <nImageBrightness> [, <bDuplexMode>
// [, <bAutomaticLoad>]]]]]]]]])
// Parameters:
// sPDFName: Name of PDF in output
// nQuality (integer - default value=90):
// Quality of the document image
// (value included between 1 and 100, ignored if nImageType=TwainBlackWhite).
// The lower the image quality will be,
// the higher the compression rate of the image will be
// and the smaller the size of the file will be.
// bUserInterface (default value=False):
// True (default value),
// if the user interface of the Twain device used must be displayed.
// When saving the document, the <BMP file> will be proposed by default.
// nImageType (default value=0):
// Type of image used to save the document
// (classified by order of color management)
// nNumberBitsPerPixel (default value=0):
// Amount of information about the colors
// (to display or print each pixel in an image)
// The higher the number of bits per pixel is,
// the sharper the color rendering will be.
// nImageResolution (default value=0):
// Number of pixels per inch (the possible values depend on the device used).
// nImageContrast (default value=0):
// Contrast of the image (value included between -1000 and 1000).
// nImageBrightness (default value=0):
// Brightness of the image (value included between -1000 and 1000).
// bDuplexMode (default value=0):  
// Used to scan a document in duplex mode,
// False (by default) to scan the document on a single side.
// bAutomaticLoad (default value=1):  
// Used to initialize the scan for a quick load
// in case of feeder,
// False (by default)
// Return value:
// Undefined type: Number of digitized pages, 0 if an error occurred
PROCEDURE TwainMultiPageToPDF(sPDFName, nQuality is int=90, ...
bUserInterface=False, nImageType=0, nNumberBitsPerPixel=0, ...
nImageResolution=0, nImageContrast=0, nImageBrightness=0, ...
bDuplexMode=False, bAutomaticLoad=True)
ListTempFiles is List of strings
// Does the destination file exist?
IF fFileExist(sPDFName) = True THEN
// Yes, delete the file
IF fDelete(sPDFName) = False THEN
// Error if the deletion failed
// Generate a PDF with the print commands
iDestination(iGenericPDF, sPDFName)
// Name of temporary destination file
sNameScannedFile is string
bResScan is boolean
nNbPages is int = 0
// A4 format
TwainScanZone(0, 0, 210, 297)
// Scan
IF nTypeImages = TwainBlackWhite THEN
// If B&W, a scan in a monochrome BMP is smaller
sNameScannedFile = fExtractPath(fTempFile(), ...
fFileName + fDrive + fDirectory) + ".BMP"
bResScan = TwainToBMP(sNameScannedFile, bUserInterface, ...
nImageType, 1, nImageResolution, nImageContrast, ...
nImageBrightness, bDuplexMode, bAutomaticLoad)
// Otherwise, in a JPG
sNameScannedFile = fExtractPath(fTempFile(), ...
fFileName + fDrive + fDirectory) + ".JPG"
bResScan = TwainToJPEG(sNameScannedFile, nQuality, bUserInterface, ...
nImageType, nNumberBitsPerPixel, nImageResolution, ...
nImageContrast, nImageBrightness, bDuplexMode, ...
// Scan OK?
IF bResScan THEN
// Yes, stores the name of the temporary image file (to delete it at the end)
Add(ListTempFiles, sNameScannedFile)
// Is it the first page?
IF nNbPages > 1 THEN
// No, therefore page break
// To print on the entire page
iPrintImage(sNameScannedFile, 0, 0, iPageWidth(), iPageHeight(), ...
// Canceled by user (on print cancel window)?
IF iDocumentCanceled() THEN BREAK
// Problem with the scan, stop?
IF NOT YesNo(ErrorInfo(), "Continue to digitize?") = False THEN BREAK
// More pages?
IF TwainState() = tsUnloaded THEN BREAK
// Yes, therefore next page
// Returns the number of scanned pages
// Was the print started?
IF nNbPages > 0 THEN
// Yes, therefore end of PDF generation (end of print)
// Delete the temporary images
FOR EACH ELEMENT sNameScannedFile OF ListTempFiles
Minimum version required
  • Version 15
This page is also available for…
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help