How to scan documents in a ASP.NET Web Application?

Sep 22, 2021
How to scan documents in a ASP.NET Web Application?

Scan document in ASP.NET Web Form

Document scanning module in ASP.NET Web Form

As you know, ASP.NET is a server-side programming language and it cannot access client-side devices. To embed a document scanning module into your ASP.NET web application, you can use a browser-based SDK to interact with the scanners. This article will show how to use the Dynamic Web TWAIN SDK to achieve that.

Capture Devices

Dynamic Web TWAIN is a document scanning SDK specifically designed for web applications. It allows you to capture images from TWAIN/WIA/SANE compatible scanners, digital cameras and capture cards.

Browsers

Dynamic Web TWAIN supports all the mainstream browsers on Windows, macOS, or Linux at the client side.

It provides a HTML5 TWAIN SDK to support scanning in modern HTML5 browsers, such as Edge, latest versions of Internet Explorer, Chrome, Firefox, and Safari. It also includes ActiveX and Plugin options to support document scanning in legacy browser versions.

How to Scan Document in ASP.NET App with Dynamic Web TWAIN?

Dynamic Web TWAIN is a client-side SDK and you can call all the APIs of Dynamic Web TWAIN in JavaScript. After scanning, browser users can save the images to local disk, web server, database or SharePoint libraries etc. Here we will show you how to upload the scanned images to web server in C# and VB.NET.

Scan Document (JavaScript)

function DWT_AcquireImage() {
if (DWT_DWTSourceContainerID == "")
    WebTWAIN.SelectSource();
else
    WebTWAIN.SelectSourceByIndex(document.getElementById(DWT_DWTSourceContainerID).selectedIndex); //display the available scanners
    WebTWAIN.CloseSource(); //to ensure the device is not in use
    WebTWAIN.OpenSource();

    WebTWAIN.IfFeederEnabled = true; //use ADF scanning
    WebTWAIN.IfShowUI = false; //hide the user interface of the scanner
    WebTWAIN.PixelType = 2;
    WebTWAIN.Resolution =200;
    WebTWAIN.AcquireImage(); //acquire images
}

Upload Image

Client-Side JavaScript

function btnUpload_onclick(){
  var strActionPage;
  var strHostIP;

  var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII
  var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
  strActionPage = CurrentPath + "SaveToFile.aspx"; //the ActionPage's file path

  strHostIP = "localhost"; //The host's IP or name

  WebTWAIN.HTTPPort = 80;
  WebTWAIN.HTTPUploadThroughPost(strHostIP,0,strActionPage,"imageData.tif");

  if (WebTWAIN.ErrorCode != 0) alert(WebTWAIN.ErrorString);
  else //succeded
  alert("Image Uploaded successfully");
}

Action Page: Upload Image (C#)

<%@ Page Language="c#" AutoEventWireup="false" Debug="True"%>;

<%
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile uploadfile = files["RemoteFile"];
uploadfile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(".") + "/" + uploadfile.FileName);
%>

Action Page: Upload Image (VB.NET)

<%@ Page Language="vb" AutoEventWireup="false"%>
<%
Dim files As HttpFileCollection = HttpContext.Current.Request.Files
Dim uploadfile As HttpPostedFile = files("RemoteFile")

dim filePath
filePath = System.Web.HttpContext.Current.Request.MapPath(".") & "/UploadedImages/"

If Not System.IO.Directory.Exists(filePath) Then
System.IO.Directory.CreateDirectory(filePath)
End If
filePath = filePath & uploadfile.FileName

uploadfile.SaveAs(filePath)
%>

Download working samples →

Let us know in the comments section below about your experience in creating your web-based ASP.NET document imaging applications.