Dynamic Web TWAIN: how to use default source

When scanning, you may be interested in either automatically using the default scanner source, or changing the default scanner source.

By default, we do not support changing the defaults source because of the DSM(Data Source Manager) that is used by default.

For displaying the default source first in a drop down menu there are a few steps that are required:

  1. Set IfUseTwainDSM to true, make sure to do this before any TWAIN related methods or properties are used
  2. Use DefaultSourceName to return the default source name
  3. Loop through each source and compare it to the default. If a match is found, set it to the default name

To save and update the default scanner using cookies you can do the following:

  1. If it is the first time that a user has visited your page do steps 1-3 as above.
  2. If the end user switches to a different scanner, save that scanner as the default scanner into a browser cookie.
  3. If the end user is visiting the site not for the first time, read the data from the browser cookie for default source

For an example of how to do this please view the code snippets below: SaveDefaultSource

function SaveDefaultSource() {
   // set period of validity for the cookie
   var date = new Date();
   var expiresDays = 30;
   date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000);
   if (DWObject) {
      DWObject.SelectSourceByIndex(document.getElementById("source").selectedIndex);
      // save related info to cookie
      document.cookie = "DefaultSourceName=" + DWObject.CurrentSourceName + ";expires=" + date.toGMTString();
   }
}

LoadDefaultSource

function LoadDefaultSource() {
   // load info from cookie as string value
   var strCookie = document.cookie;
   // check if a pre-scan settings has been saved before
   if(strCookie != "") {
      // spilt the cookie string into several name/value pair
      var arrCookie = strCookie.split("; ");
      // iterate through the array
      // locate the cookie named "DefaultSourceName", and get the value
      for (var i = 0; i < arrCookie.length; i++) {
         var arr = arrCookie[i].split("=");
         if ("DefaultSourceName" == arr[0]) {
            DWObject.IfUseTWAINDSM = true;
            DWObject.DefaultSourceName = arr[1];
            var sourceList = document.getElementById("source");
            for (var j = 0; j < sourceList.length; j++) {
               if(document.getElementById("source").options[j].innerText == arr[1]){
                  document.getElementById("source").options.selectedIndex = j;
               }
            }      
         break;
         }
      }
   }
}

deleteCookie

function deleteCookie() {
   var cookieCt = 0;
   if (document.cookie != "") {
      var thisCookie = document.cookie.split("; ");
      cookieCt = thisCookie.length;
      var expireDate = new Date();
      expireDate.setDate(expireDate.getDate()-1);     
      for (var i=0;i < cookieCt; i++) {
         var cookieName = thisCookie[i].split("=")[0];
         if(cookieName == "DefaultSourceName"){
         document.cookie = cookieName + "=;expires=" + expireDate.toGMTString();
         }
      }
   }
}

Please download the sample code via the link below: Set Default Source

Is this page helpful?

Leave a Reply

Your email address will not be published.