An open API service indexing awesome lists of open source software.

https://github.com/dynamsoft/dynamic-web-twain-rest-dotnet

.NET packages for using Dynamic Web TWAIN's RESTful API to scan documents
https://github.com/dynamsoft/dynamic-web-twain-rest-dotnet

document-scanning dotnet maui restful-api winforms wpf

Last synced: 26 days ago
JSON representation

.NET packages for using Dynamic Web TWAIN's RESTful API to scan documents

Awesome Lists containing this project

README

          

# Dynamic Web TWAIN REST .NET

This repo contains the packages for scanning documents using [Dynamic Web TWAIN](https://www.dynamsoft.com/web-twain/overview/)'s [REST API](https://www.dynamsoft.com/web-twain/docs/extended-usage/restful-api.html).

There are four packages:

* **DynamicWebTWAIN.RESTClient**: the .NET wrapper of the REST API.
* **DynamicWebTWAIN.Service**: a package to help you embed Dynamic Web TWAIN service in your app without the need to use its installer. The service is used to access document scanners and provide the REST API for document scanning.
* **DynamicWebTWAIN.ServiceFinder**: a package to help you find running Dynamic Web TWAIN services in a local network.
* **DocumentViewer.JSInterop**: a package to help you integrate document capturing with a dedicated viewer in your .NET app using WebView with extra features like PDF loading, annotation, and saving. It is using a web document viewer library: [Dynamsoft Document Viewer](https://www.dynamsoft.com/document-viewer/overview/).

The whole solution enables developers to create desktop or cross-platform applications to scan and digitize documents using the following scanning protocols:

* TWAIN (32-bit / 64-bit)
* WIA (Windows Image Acquisition)
* SANE (Linux)
* ICA (macOS)
* eSCL (AirScan / Mopria)

## Requirements

* .NET Standard 2.0 or greater
* .NET 4.6.1 (Desktop / Server) or greater
* License for Dynamic Web TWAIN: [30-day trial license](https://www.dynamsoft.com/customer/license/trialLicense?product=dwt)
* (Optional) License for Dynamsoft Document Viewer if using the document viewer: [30-day trial license for both Web TWAIN and Document Viewer](https://www.dynamsoft.com/customer/license/trialLicense?product=dwtddv)

To open the samples, you need Visual Studio 2022 and .NET 9+.

## Installation

You need to reference the projects and add them as dependencies.

If you need to copy the service files and web resources (for MAUI) in the service package to your project, you need to add the following to your project.

```xml

```

## Usage

### Scan Documents

1. Create a REST client instance. You need to specify the service's IP and the license key of Dynamic Web TWAIN ([apply for a 30-day trial](https://www.dynamsoft.com/customer/license/trialLicense/?product=dwt)).

```csharp
var address = "https://127.0.0.1:18623";
var license = "LICENSE-KEY";
var client = new DWTClient(new Uri(address), license);
```

This requires you to install Dynamic Web TWAIN service beforehand ([download](#web-twain-service-installers)).

You can also embed the service in your app by using the service package (no installation of service is required using this way). Currently, only Windows is supported.

```csharp
var serviceManager = new ServiceManager();
serviceManager.CreateService(); //create a service instance
var address = serviceManager.Service.BaseAddress; //get the address for the REST client to use
```

2. List connected scanners.

```csharp
var scanners = await client.ScannerControlClient.ScannerManager.GetScanners(EnumDeviceTypeMask.DT_TWAINSCANNER | EnumDeviceTypeMask.DT_WIASCANNER | EnumDeviceTypeMask.DT_ICASCANNER | EnumDeviceTypeMask.DT_SANESCANNER);
```

3. Start a job to scan documents using a scanner.

```csharp
CreateScanJobOptions options = new CreateScanJobOptions();
options.AutoRun = false;
options.Device = scanners[0].Device;
options.Config = new ScannerConfiguration();
options.Config.IfFeederEnabled = true;
options.Config.IfDuplexEnabled = true;
var job = await client.ScannerControlClient.ScannerJobs.CreateJob(options);
await job.StartJob();
```

4. Retrieve the scanned document images one by one.

```csharp
do
{
byte[] result = await job.GetNextImage();
if (result == null)
{
await job.DeleteJob();
break;
}
else
{
//process the image bytes
}
} while (true);
```

### Use the Document Viewer

We can embed [Dynamsoft Document Viewer](https://www.dynamsoft.com/document-viewer/docs/introduction/index.html) in a WebView to view and edit the scanned document images and save the images as PDF.

1. Add a WebView in your app, like WebView2 for WinForm/WPF and HybridWebView for MAUI.

2. Implement the `IWebViewBridge` interface for different WebViews. The following is some examples.

MAUI HybridWebView:

```csharp
public class HybridWebViewBridge : IWebViewBridge
{
private Microsoft.Maui.Controls.HybridWebView _webView;

public HybridWebViewBridge(Microsoft.Maui.Controls.HybridWebView webView)
{
_webView = webView;

}

public async Task ExecuteJavaScriptAsync(string script)
{
string result = null;
await MainThread.InvokeOnMainThreadAsync(async () =>
{
result = await _webView.EvaluateJavaScriptAsync(script);
});
return result;
}

public void RegisterCallback(Func callback)
{
_webView.RawMessageReceived += (sender, args) =>
{
callback?.Invoke(args.Message);
};
}

public async Task LoadUrlAsync(Uri url)
{
var tcs = new TaskCompletionSource();

_webView.DefaultFile = url.OriginalString;

await Task.CompletedTask;
}
}
```

WPF WebView2:

```csharp
public class WpfWebViewBridge : IWebViewBridge
{
private Microsoft.Web.WebView2.Wpf.WebView2 _webView;

public WpfWebViewBridge(Microsoft.Web.WebView2.Wpf.WebView2 webView)
{
_webView = webView;
}

public async Task ExecuteJavaScriptAsync(string script)
{
string result = null;
await Application.Current.Dispatcher.InvokeAsync(async () =>
{
result = await _webView.ExecuteScriptAsync(script);
});
return result;
}

public void RegisterCallback(Func callback)
{
_webView.CoreWebView2.WebMessageReceived += (sender, args) =>
{
callback?.Invoke(args.TryGetWebMessageAsString());
};
}

public async Task LoadUrlAsync(Uri url)
{
_webView.Source = url;
await Task.CompletedTask;
}
}
```

3. Copy the web page.

For WebView2, if you are using the service package, the web page already exists in the service's [folder](./DynamicWebTWAIN.Service/PackService/content/common/dynamsoft.dwt.service/app/site/default/) and we can load it via URL. If you installed the service via the installers, you need to copy the web page to the service's `app/site/default/` folder.

For HybridWebView, you need to copy the web page to the `Resources/raw/ddv` folder. If you've added the following to your project, it will copy the web page packed in the service package to your app's root.

```xml


```

4. Create an instance of JSInterop. You need to use a license which is for both Dynamic Web TWAIN and Dynamosft Document Viewer ([apply for a 30-day trial](https://www.dynamsoft.com/customer/license/trialLicense?product=dwtddv)).

```csharp
private JSInterop _jsInterop = null;
JSInteropOptions options = new JSInteropOptions();
options.ProductKey = "ProductKey";
_jsInterop = new JSInterop(options,
new WpfWebViewBridge(webView)
new Uri("https://127.0.0.1:18623")); //URL of the Dynamic Web TWAIN service
await _jsInterop.EnsureInitializedAsync();
```

5. Scan documents into the viewer.

```csharp
CreateScanJobOptions options = new CreateScanJobOptions();
options.AutoRun = false;
var scannerJob = await _jsInterop.CreateScanToViewJob(options);
await _jsInterop.StartJob(scannerJob);
```

6. Save the documents as PDF.

```csharp
PageOption pageOption = PageOption.All;
PdfPageType pdfPageType = PdfPageType.PageDefault;
SaveAnnotationMode annotationMode = SaveAnnotationMode.None;
byte[] pdfContent = await _jsInterop.SaveAsPdf(pageOption,pdfPageType,annotationMode,"");
```

## Samples

With Dynamsoft Document Viewer in a WebView:

* [WPFDemo](./Samples/WithDocumentViewer/WpfDemo/): a full-featured demo in WPF with various image editing, PDF annotation and saving options.
* [MAUIDemo](./Samples/WithDocumentViewer/MAUIDemo/): a full-featured demo in MAUI that is mainly designed for the mobile platform.
* [MAUIHybridApp](./Samples/WithDocumentViewer/MauiHybridApp/): a basic MAUI sample that scans document images into a viewer.
* [WPFWebViewApp](./Samples/WithDocumentViewer/WpfWebviewApp/): a basic WPF sample that scans document images into a viewer.
* [WinFormsApp](./Samples/WithDocumentViewer/WinFormsApp/): a basic WinForms sample that scans document images into a viewer.

Without Dynamsoft Document Viewer:

* [ConsoleApp](./Samples/WithoutDocumentViewer/ConsoleApp/): a basic console sample that scans document images into a PDF file.
* [WpfApp](./Samples/WithoutDocumentViewer/WpfApp/): a basic WPF sample that scans document images into a PDF file.

## Links

* [Dynamsoft Document Viewer Documentation](https://www.dynamsoft.com/document-viewer/docs/introduction/index.html)
* [RESTful API Documentation](https://www.dynamsoft.com/web-twain/docs/extended-usage/restful-api.html)
* [Twain.Wia.Sane.Scanner](https://www.nuget.org/packages/Twain.Wia.Sane.Scanner): Another wrapper of the REST API published on nuget

## Web TWAIN Service Installers

| Platform | Download Link |
| ------------- | --------------- |
| Windows | [Dynamic-Web-TWAIN-Service-Setup.msi](https://demo.dynamsoft.com/DWT/DWTResources/dist/DynamicWebTWAINServiceSetup.msi) |
| macOS | [Dynamic-Web-TWAIN-Service-Setup.pkg](https://demo.dynamsoft.com/DWT/DWTResources/dist/DynamicWebTWAINServiceSetup.pkg) |
| Linux | [Dynamic-Web-TWAIN-Service-Setup.deb](https://demo.dynamsoft.com/DWT/DWTResources/dist/DynamicWebTWAINServiceSetup.deb)
[Dynamic-Web-TWAIN-Service-Setup-arm64.deb](https://demo.dynamsoft.com/DWT/DWTResources/dist/DynamicWebTWAINServiceSetup-arm64.deb)
[Dynamic-Web-TWAIN-Service-Setup-mips64el.deb](https://demo.dynamsoft.com/DWT/DWTResources/dist/DynamicWebTWAINServiceSetup-mips64el.deb)
[Dynamic-Web-TWAIN-Service-Setup.rpm](https://demo.dynamsoft.com/DWT/DWTResources/dist/DynamicWebTWAINServiceSetup.rpm)|