Adding/Removing Dynamic Web TWAIN object individually

Starting with version 12.1, you can dynamically add/remove a specified Dynamic Web TWAIN object by using the following methods

Dynamsoft.WebTwainEnv.CreateDWTObject(id, ip, Port, SSLPort, OnSuccessCallback, OnFailureCallback)
Dynamsoft.WebTwainEnv.DeleteDWTObject(id)

The first method is pretty self-explanatory—it helps us to create a new Dynamic Web TWAIN object by passing in the id of the DWT container(generally, a div element on the page) as well as the IP and Ports the new object should be connecting to. The second allows us to remove a Dynamic Web TWAIN object from the web page by specifying its container id.

 

Add a Dynamic Web TWAIN object at runtime

 

To add a Dynamic Web TWAIN object on the web page, you should put a div element as the container for Dynamic Web TWAIN:

<div id="dwtcontrolContainer2"></div>

The next step is to use the Dynamsoft.WebTwainEnv.CreateDWTObject method to create and initialize a new Dynamic Web TWAIN object that is embedded in the div with id 'dwtcontrolContainer2'. Here’s what it looks like in code:

var DWObject2; 
Dynamsoft.WebTwainEnv.CreateDWTObject( 
  "dwtcontrolContainer2", "192.168.3.1", 18618, 18619,
  function(DWTObject){ 
    DWObject2 = DWTObject;
  }, 
  function(errorString){ 
    alert(errorString);
} );

Please NOTE that the new object is initialized and then passed to the callback function OnSuccessCallback where you can assign it to a variable like DWObject2. Then, you can start to manipulate the new object. For example:

DWObject2.Width = 580;
DWObject2.Height = 600;
DWObject2.SelectSource();
DWObject2.AcquireImage();

NOTE:

  1. The ip, Port and SSLPort parameters you use to create a new DWT object should match the settings used by the Dynamic Web TWAIN service you are connecting to. For example, the file DWTConfiguration.ini for the above new object should look something like this

    EnableScannerAccessAuth=TRUE
    EnableFileAccessAuth=TRUE
    EnableWebcamAccessAuth=TRUE
    LogLevel=6
    Server=192.168.3.1
    Port=18618
    PortSSL=18619
    

    Also, make sure the ip is accessible from the current machine.

  2. If the div element with id “dwtcontrolContainer2” already exists in the Dynamsoft.WebTwainEnv.Containers in the file dynamsoft.webtwain.config.js like the following

    Dynamsoft.WebTwainEnv.Containers = [{ContainerId:'dwtcontrolContainer', Width: 120, Height: 350}, {ContainerId:'dwtcontrolContainer2', Width: 270, Height: 350}];
    

    you will get the following error:

    Duplicate ID detected for creating Dynamic Web TWAIN objects, please check and modify.

    In this case, please change or remove the following from dynamsoft.webtwain.config.js

    {ContainerId:'dwtcontrolContainer2', Width: 270, Height: 350}
    

Remove a Dynamic Web TWAIN object at runtime

To remove a Dynamic Web TWAIN object from the web page, it is quite easy. You just need to call the Dynamsoft.WebTwainEnv.DeleteDWTObject(id) method by providing the container id.

Dynamsoft.WebTwainEnv.DeleteDWTObject("dwtcontrolContainer2");

NOTE:
Dynamsoft.WebTwainEnv.Unload() can’t release the Dynamic Web TWAIN object generated by Dynamsoft.WebTwainEnv.CreateDWTObject(id, OnSuccessCallback, OnFailureCallback). You can only use the Dynamsoft.WebTwainEnv.DeleteDWTObject(id) method to release that object.