Properties/Methods/Events

When properly implemented, Dynamic Web TWAIN will automatically initialize right after the page is loaded. Once the initialization, you can control it like any normal JS object. You can refer to our online API Documentation to check all built-in properties, methods and events of Dynamic Web TWAIN.

Basically, there are 3 ways to use Dynamic Web TWAIN:

Properties

Properties are used to get or set a certain value in the Dynamic Web TWAIN object at runtime such as ResolutionDuplexIfShowUI, etc.

/* Property */
/* Scan pages in 200 DPI */
DWObject.Resolution = 200;

Methods

Methods are used to call the built-in functions of the Dynamic Web TWAIN object such as AcquireImageSaveAsJPEGRotate, etc. The syntax is fairly simple:

// Method
 
///
<summary> /// Rotates the image of a specified index in buffer by a specified angle.
  /// </summary>
///
<param name="sImageIndex" type="short">
specifies the index of image in buffer. The index is 0-based.
</param>
///
<param name="fAngle" type="float">
specifies the angle.
///
<param name="bKeepSize" type="bool">
specifies whether to keep the original size
///
<returns type="bool"></returns>
DWObject.Rotate(0, 45, false); // rotate the 1st image in the buffer by 45 degrees

Events

Events are triggered when certain trigger points are reached. For example, we have an OnMouseClick event for mouse clicking, an OnPostTransfer event for the end of transferring one image, etc. Compared with Properties and Methods, Events are a little bit tricky to use. Here we’ll talk about it a little more.

Handling Events

Add an event listener

To add an event listener, you can use the built-in method RegisterEvent. Please refer to the sample code below:

 
<script type="text/javascript">
Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);
var DWObject; 
/* OnWebTwainReady event fires as soon as Dynamic Web TWAIN is initialized and ready to be used. It is the best place to add event listeners */
function Dynamsoft_OnReady() {
   DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
   DWObject.RegisterEvent("OnPostTransfer", Dynamsoft_OnPostTransfer);
}
function Dynamsoft_OnPostTransfer() { 
   /* This event OnPostTransfer will be triggered after a transfer ends. */
   /* your code goes here*/
}
</script> 

In the above code, we added the JavaScript function Dynamsoft_OnPostTransfer() as an event listener for the event OnPostTransfer. Alternatively, you can write code like this:

 
<script type="text/javascript">
Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);
var DWObject; 
function Dynamsoft_OnReady() {
   DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
   DWObject.RegisterEvent("OnPostTransfer", function() { 
	/* your code goes here*/
   });
}
</script> 

Event with parameter(s)

Some of the events have parameter(s). Take OnMouseClick Event as an example:

/* sImageIndex is the index of the image you clicked on*/
OnMouseClick(short sImageIndex) 

When you create the corresponding JavaScript function (A.K.A., the event listener), you can include the parameter(s) and retrieve the value at runtime.

function DynamicWebTwain_OnMouseClick(index) {
    CurrentImage.value = index + 1;
}

Or

DWObject.RegisterEvent("OnPostTransfer", function(index) { 
    CurrentImage.value = index + 1;
});

Special Event - 'OnWebTwainReady'

Of all the built-in events, there is one called 'OnWebTwainReady' which is special. Basically this event fires as soon as the Dynamic Web TWAIN object is initialized and ready to be used. As you have seen earlier in the document, the recommended way to use it is:

<script type="text/javascript">
Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);
var DWObject; 
function Dynamsoft_OnReady() {
   DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
}
</script>

Or

<script type="text/javascript">
   Dynamsoft.WebTwainEnv.RegisterEvent('OnWebTwainReady', function() {
     DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
   });
</script>
Is this page helpful?

Leave a Reply

Your email address will not be published.