4

I've been assigned to a project which involves using a document scanning API to obtain user documents.The application will be written in C# and targets the .NET framework running on Windows desktop.

From what I've gathered, the sensible choice is to use a TWAIN library. However, I'm not sure whether to go with open-source libraries such as dotnettwain and (the more recently updated) NTwain, or commercial options such as dynamsoft and Atalasoft.

I'm not concerned with feature comparison as my requirements are likely to be very basic. My main concern is developer time, and I'm perplexed by Ntwain's readme which states

To properly use this lib you will need to be reasonably familiar with the TWAIN spec and understand how it works in general

and

There are no high-level, scan-a-page-for-me-now functions

On the other hand, the mentioned commercial solutions seem to work with old versions of the TWAIN spec and don't seem to be very actively developed.

1

If you are looking for a commercial library that offers high level scanning to PDF I would recommend taking a look at the LEADTOOLS TWAIN SDK. This SDK is compatible with versions 1.X and 2.X of TWAIN. Scanning an image to PDF would be as simple as the following:

  twnSession = new TwainSession();
  twnSession.Startup(this.Handle, "", "", "", "", TwainStartupFlags.None);
  twnSession.SelectSource(string.Empty);
  twnSession.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(twnSession_AcquirePage);

This starts a twain session, and prompts a dialogue to choose the scanning device (you can specify the scanner programmatically if you don't want a dialog). For each page scanned and aquire event is fired, and in this event you can save it as a PDF file.

Here is some example code on how to save out the image as PDF:

 private void twnSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
  {
      using(RasterCodecs codecs = new RasterCodecs())
      {
          codecs.Save(e.Image, @"Test.PDF", RasterImageFormat.RasPdf, 0, 1, -1,1, CodecsSavePageMode.Append)
       }
  }

Please note I am an employee of this tool.

0

From a quick look at the products the open source libraries are trying to implement the twain spec while the commercial products enable you to call the twain driver.

The latter is more useful to you I guess. Here is a more up-to-date project that supports TWAIN specification 1.x / 2.x: https://sarafftwain.codeplex.com/

0

64bit Apps Access 32bit TWAIN Drivers

The tricky part is that most vendors supply 32bit TWAIN drivers only. If you are developing 64bit applications, do consider to use Asprise C# VB.NET scanning SDK which allows you to access TWAIN scanners from x64 applications correctly - It also offers features like ADF bulk scan to multi-page PDF and barcode reading recognition.

You don't have to understand the TWAIN spec

I'm not concerned with feature comparison as my requirements are likely to be very basic. My main concern is developer time

Bellow is the actual code prompts for device selection and perform TWAIN scans and save to multi-page PDF file:

using asprise_imaging_api;

Result result = new AspriseImaging().Scan(new Request()
    .SetTwainCap(TwainConstants.ICAP_PIXELTYPE, TwainConstants.TWPT_RGB)
    .SetPromptScanMore(true) // prompt to scan more pages
    .AddOutputItem(new RequestOutputItem(AspriseImaging.OUTPUT_SAVE, AspriseImaging.FORMAT_PDF)
      .SetSavePath(".\\${TMS}${EXT}")), // Environment variables in path will be expanded
  "select", true, true); // "select" prompts device selection dialog.

List<string> files = result == null ? null : result.GetImageFiles();
Console.WriteLine("Scanned: " + string.Join(", ", files == null ? new string[0] : files.ToArray()));

Alternatively, you may use the C# VB.NET TWAIN scanning DSL:

Result result = new AspriseImaging().Scan("{" +
    "  \"twain_cap_setting\" : {" +
    "    \"ICAP_PIXEXELTYPE\" : \"TWPT_RGB\"" +
    "  }," +
    "  \"prompt_scan_more\" : true," +
    "  \"output_settings\" : [ {" +
    "    \"type\" : \"save\"," +
    "    \"format\" : \"pdf\"," +
    "    \"save_path\" : \".\\\\${TMS}${EXT}\"" +
    "  } ]" +
    "}", "select", true, true);

Should you decide to create your own library, this is good starting point: github.com/tmyroadctfig/twaindotnet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.