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 Forms

Integrating document scanning capabilities into your ASP.NET web applications can significantly enhance their functionality. However, ASP.NET, being a server-side framework, does not have direct access to client-side devices such as scanners. To bridge this gap, you can leverage a browser-based SDK like Dynamic Web TWAIN.

Capture Devices

Dynamic Web TWAIN is a robust document scanning SDK designed for web applications. It supports a wide range of devices, including TWAIN/WIA/SANE-compatible scanners, digital cameras, and capture cards, making it a versatile choice for your document scanning needs.

Browser Compatibility

The SDK is compatible with all mainstream browsers on Windows, macOS, and Linux. For modern HTML5 browsers like Edge, Internet Explorer, Chrome, Firefox, and Safari, Dynamic Web TWAIN provides an HTML5 TWAIN SDK. Additionally, it offers ActiveX and Plugin options to support document scanning in older browser versions.

Implementing Document Scanning in an ASP.NET Application with Dynamic Web TWAIN

Since Dynamic Web TWAIN is a client-side SDK, you can interact with it using JavaScript. After scanning, users can save the images to various destinations, such as the local disk, web server, database, or SharePoint libraries. Below, we demonstrate how to upload scanned images to a web server using C# and VB.NET.

Scanning Documents (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 →

We hope this guide helps you integrate document scanning capabilities into your ASP.NET web applications seamlessly.