Use Dynamsoft to Store and Retrieve Scanned Images from SQL Server in ASP.NET

The ability to easily store and retrieve digitized documents is an important feature of any document management workflow process. In this article, we will illustrate how to use document scan features in an ASP.NET web application to save scanned files as PDFs to an SQL Server database. We’ll also cover retrieval of image files and their display within your ASP.NET web page.

We will be using Dynamic Web TWAIN, which is a TWAIN ActiveX/Plug-in for web applications, to expedite the development of document scanning, uploading and display capabilities.

Key Features

First, it’s important to point out key features within Dynamic Web TWAIN that streamlines the development process:

  • Compatible with the mainstream browsers including IE, Firefox, Chrome, Safari and Opera on Windows & Mac OS X
  • Capable of scanning images from scanners and other TWAIN compatible devices
  • Support for BMP, JPEG, PNG, single/multi-page PDF and single/multi-page TIFF
  • Support for HTTPS uploading
  • Support for cookies & sessions
  • Support for uploading extra text alongside respective images

Using the Code

Document Scanning

As Dynamic Web TWAIN is a client-side SDK, we will use JavaScript to call its methods/properties. With the SDK, you can customize the scan settings such as resolution, pixel type, brightness, contrast, page size, etc. However, in this article we will focus on aspects of storing and retrieving images from SQL Server. To this end, we will only cover a simple scan process.

function acquireImage() {
    if (_divDWTSourceContainerID == "")
        DWObject.SelectSource();
    else
        DWObject.SelectSourceByIndex(document.getElementById(_divDWTSourceContainerID).selectedIndex); //select a TWAIN scanner
    DWObject.CloseSource(); //make sure the source is closed before using it
    DWObject.OpenSource();
    DWObject.IfShowUI = document.getElementById("ShowUI").checked; //show or hide the user interface of the TWAIN scanner

    var i;
    for (i = 0; i < 3; i++) {
        if (document.getElementsByName("PixelType").item(i).checked == true)
            DWObject.PixelType = i;
    } // set the pixel type of the acquired images, B/W, gray or color
    DWObject.Resolution = document.getElementById("Resolution").value; //set the resolution
    DWObject.IfFeederEnabled = document.getElementById("ADF").checked; //scan images from auto feeder
    DWObject.IfDuplexEnabled = document.getElementById("Duplex").checked; //enable duplex scanning
    appendMessage("Pixel Type: " + DWObject.PixelType + "<br />Resolution: " + DWObject.Resolution + "<br />");

    DWObject.IfDisableSourceAfterAcquire = true;
    DWObject.AcquireImage(); //start document scanning
}

Save Scanned Images as a Multi-Page PDF to SQL Server

After scanning you can save images in many file formats: BMP, PNG, JPG, TIF and PDF. Multi-page TIF and PDF are also supported. In this example, we will save the images as a multi-page PDF file. The respective code is as follows:

function btnUpload_onclick() {
    if (!checkIfImagesInBuffer()) {
        return;
    }
    var i, strHTTPServer, strActionPage, strImageType;
    _txtFileName.className = "";
    if (!strre.test(_txtFileName.value)) {
        _txtFileName.className += " invalid";
        _txtFileName.focus();
        appendMessage("Please input file name./*Currently only English names are allowed.*/");
        return;
    }
    //DWObject.MaxInternetTransferThreads = 5;
    strHTTPServer = _strServerName;
    DWObject.HTTPPort = _strPort;
    var CurrentPathName = unescape(location.pathname); // get current PathName in plain ASCII	
    var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
    strActionPage = CurrentPath + _strActionPage; // the aspx page for receiving image data on the server side
    var redirectURLifOK = CurrentPath + "online_demo_list.aspx";  
    var uploadfilename = _txtFileName.value + "." + document.getElementsByName("ImageType").item(i).value;

    DWObject.HTTPUploadAllThroughPostAsPDF(
            strHTTPServer,
            strActionPage,
            uploadfilename
        ); //upload images as multi-page PDF file

    _strTempStr = _strTempStr + "Upload: ";
    if (checkErrorString()) {
        if (strActionPage.indexOf("SaveToFile") != -1)
            alert(DWObject.ErrorString)//if save to file.
        else
            window.location = redirectURLifOK;
    }
}

The article was posted on CodeProject. Click to read more

 

 

  • Jonathan Alcantara

    how does it works if my server is on “https”?, because i tried this and does not work:

    DWObject.HTTPUploadAllThroughPostAsPDF(
    “https://”+strHTTPServer,
    strActionPage,
    uploadfilename

    );

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com