How to save scanned documents with Dynamic Web Twain

1.6k views Asked by At

I'm using Dynamic web twain on my web app that uses angular 4 and I have trouble saving documents after the scan I found this function in Twain documentation :

function DynamicWebTwain_OnPostTransfer() { //fires after each scan
     var strFileName;
     var Digital = new Date();
     var Month = Digital.getMonth() + 1;
     var Day = Digital.getDate();
     var Hour = Digital.getHours();
     var Minute = Digital.getMinutes();
     var Second = Digital.getSeconds();
     var CurrentTime = Month + "_" + Day + "_" + Hour + "_" + Minute + "_" + Second;
     strFileName = "D:/temp/"+CurrentTime + ".pdf";
     DWObject.SaveAsPDF(strFileName,DWObject.CurrentImageIndexInBuffer); //save each scanned image as a different PDF file 
     if (DWObject.ErrorCode != 0) {  
         alert (DWObject.ErrorString);
     }
 }

But the problem is that I don't know where to put it in my twain.js and how is it supposed to work Here's my twain.js

var app = angular.module('WebScanning', []);
app.controller('twainControl',function twainControl($scope) {
$scope.acquireImage = function() {
    var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer'); // Get the Dynamic Web TWAIN object that is embeded in the div with id 'dwtcontrolContainer'.
    DWObject.IfDisableSourceAfterAcquire = true;    // Source will be closed automatically after acquisition.
    DWObject.SelectSource();                        // Select a Data Source (a device like scanner) from the Data Source Manager.
    DWObject.OpenSource();                          // Open the source. You can set resolution, pixel type, etc. after this method. Please refer to the sample 'Scan' -> 'Custom Scan' for more info.
    DWObject.AcquireImage();

};

});

any idea?

1

There are 1 answers

0
Tom K On

First of all, for Angular 4 or 5, a more recommended code sample is this one which includes all the common features including scan/load/save/upload, etc. The way you are writing your code is more like the old Angular JS style.

That said. the following snippet should work for your current code

    $scope.acquireImage = function() {
        var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer'); 
        DWObject.RegisterEvent('OnPostTransfer', ()=>{
            var strFileName;
            var Digital = new Date();
            var Month = Digital.getMonth() + 1;
            var Day = Digital.getDate();
            var Hour = Digital.getHours();
            var Minute = Digital.getMinutes();
            var Second = Digital.getSeconds();
            var CurrentTime = Month + "_" + Day + "_" + Hour + "_" + Minute + "_" + Second;
            strFileName = "D:/temp/"+CurrentTime + ".pdf";
            DWObject.IfShowFileDialog=false;
            DWObject.IfShowProgressBar = false;
            console.log(DWObject.CurrentImageIndexInBuffer);
            DWObject.SaveAsPDF(strFileName,DWObject.CurrentImageIndexInBuffer, function(){},function(){}); //save each scanned image as a different PDF file 
            if (DWObject.ErrorCode != 0) {  
                alert (DWObject.ErrorString);
            }
        });
        DWObject.IfDisableSourceAfterAcquire = true;    // Source will be closed automatically after acquisition.
        DWObject.SelectSource();                        // Select a Data Source (a device like scanner) from the Data Source Manager.
        DWObject.OpenSource();                          // Open the source. You can set resolution, pixel type, etc. after this method. Please refer to the sample 'Scan' -> 'Custom Scan' for more info.
        DWObject.AcquireImage();