C#: Online Guide
How To: Twain SDK
Scan Multi-pages into One PDF Document
Scan Multi-pages into One PDF Document
  |  
Home ›› XImage.Twain ›› C# Twain: Scan Multi-pages into One PDF Document

C# TWAIN - Acquire or Save Image to File


How to Save Acquired Image to File in C#.NET with TWAIN Add-on




Overview - Save Image to File with C# TWAIN Device



RasterEdge C#.NET TWAIN Add-On enables developers to directly acquire image to file with a couple of code lines in C# programming project. Using this C#.NET TWAIN Add-On, you are capable of scanning images from digital imaging devices (such as TWAIN compatible scanners and digital cameras) automatically and saving the images to file in C#.NET application.


Related .net document control helps:
asp.net mvc image viewer: ASP.NET Image Viewer Control(MVC & WebForms): view, annotate, redact, convert image files in html, JQuery
asp.net dicom library: ASP.NET Dicom Document Viewer Control: view, annotate dicom imaging files online in ASP.NET
asp.net document viewer example: EdgeDoc:ASP.NET Document Viewer C# Control: Open, view, annotate, redact, convert documents online in C#, VB.NET, AS...
asp.net edit pdf image control: ASP.NET PDF Image Edit Control: online insert, edit PDF images in C#
asp.net edit pdf page control: ASP.NET PDF Pages Edit Control: add, remove, sort, replace PDF pages online using C#
asp.net view excel in browser: ASP.NET Excel Viewer in C# Control (MVC & WebForms): view Office Excel document in web browser.
asp.net sharepoint document viewer control: ASP.NET SharePoint Document Viewer: view, annotate, redact documents in SharePoint


If you want to acquire an image directly into the C#.NET application in which you are going to work with the image, you'd better use the widely used application programming interface - TWAIN. TWAIN is not a hardware-level protocol, but a communication protocol that realizes connection between software and digital imaging devices. Our C#.NET TWAIN Add-On supports acquiring or saving image to file by using compression algorithm supported by the device.




C# TWAIN Scanning DLLs: Scan Many Pages into One PDF



In order to scan multiple pages into one PDF document through C#.NET programming, you may need to add the following dlls to your C# TWAIN scanning project.



RasterEdge.XImage.BarcodeScanner.dll


RasterEdge.Imaging.Basic.dll


RasterEdge.XImage.Raster.dll


RasterEdge.XImage.Raster.Core.dll


RasterEdge.Imaging.Basic.Codec.dll


RasterEdge.XDoc.PDF.dll


RasterEdge.Imaging.Drawing.dll


RasterEdge.Imaging.Font.dll


RasterEdge.Imaging.Processing.dll


RasterEdge.Imaging.WinformsControl.Twain.dll


or RasterEdge.Imaging.TIFF.dll: this is only needed for scanning many pages to TIFF file




C# TWAIN Scanning Events: Scan Many Pages into One PDF



To accomplish this kind of C#.NET TWAIN scanning, it is indispensible to set up some events to assist with the process of scanning from device and convert the images obtained to form a PDF document.


AcquireCanceled: Raised if C# user cancels TWAIN acquisition process.


AcquireCompleted: Raised when the scanner has finished scanning the last page.


ImageAcquired: Raised each time the scanner finishes scanning a page.




C# TWAIN Scanning Demo Code: Scan Many Pages into One PDF



C# developers can use the following demo code to scan multiple pages into one PDF document in C#.NET project within seconds. Note: you need to reference above mentioned DLL assemblies to your project to complete the following functions.




private bool _acquireCanceled;
private List<REImage> _imageList;

public void ScanImages()
   {
      _acquireCanceled = false;
      Acquisition myAcquisition = new Acquisition();
      myAcquisition.AcquireCanceled += new AcquireCanceledEventHandler(OnAcquireCanceled);
      myAcquisition.AcquireCompleted += new AcquireCompletedEventHandler(OnAcquireCompleted);
      myAcquisition.ImageAcquired += new ImageAcquiredEventHandler(OnImageAcquired);

      TWAINDevice device = myAcquisition.DefaultDevice;
      device.Open();
      device.ScanSetting.ShouldTransferAllPages = true;
      _imageList = new List<REImage>();
      device.Acquire();
   }

private void OnImageAcquired(object sender, ImageAcquiredEventArgs args)
   {
      if (args.OutputImage != null)
         {
            // add scaned images to image collection
            _imageList.Add(args.OutputImage);
         }
   }

private void OnAcquireCanceled(object sender, EventArgs args)
   {
      _acquireCanceled = true;
   }

private void OnAcquireCompleted(object sender, AcquireCompletedEventArgs args)
   {
      if (_acquireCanceled)
      return;

      // form a PDF document using image collection scanned
      PDFDocument doc = new PDFDocument(_imageList.ToArray());

      // save PDF document
      doc.Save(@"c:\ScannedPDF.pdf");
      doc.Dispose();

      // do some extra work to finish composing the PDF document

      //// get a TIFF document using similar operation
      // TIFFDocument doc = new TIFFDocument(_imageList.ToArray());

      //// save TIFF document
      // doc.Save(@"c:\scannedTiff.tif");
      // doc.Dispose();
   }