XDoc.PDF
Features
Tech Specs
How-to C#
How-to VB.NET
Pricing
C# PDF: How to PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
Twain
  |  
Home ›› XDoc.PDF ›› C# PDF: Twain

C#.NET PDF - Acquire or Save PDF Image to File in C#.NET


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



Look for HTML5 PDF Editor?

EdgePDF: ASP.NET PDF Editor is the best HTML5 PDF Editor and ASP.NET PDF Viewer based on XDoc.PDF, JQuery, HTML5. It supports ASP.NET MVC and WebForms projects.


Overview



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.


Using our 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.




C#.NET 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.


Add necessary references:


  RasterEdge.Imaging.Basic.dll


  RasterEdge.Imaging.Basic.Codec.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XImage.Raster.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XDoc.PDF.dll


  RasterEdge.Imaging.WinformsControl.Twain.dll


Use corresponding namespaces;


  using RasterEdge.Imaging.Raster.Core;


  using RasterEdge.XDoc.PDF;


  using RasterEdge.Imaging.WinformsControl.Twain;


  using RasterEdge.XImage.Raster;


Note: When you get the error "Could not load file or assembly 'RasterEdge.Imaging.Basic' or any other assembly or one of its dependencies. An attempt to load a program with an incorrect format", please check your configure as follows:

       

       If you are using x64 libraries/dlls, Right click the project -> Properties -> Build -> Platform target: x64.

       

       If using x86, the platform target should be x86.




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



To accomplish this kind of C#.NET TWAIN scanning, it is indispensable 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#.NET TWAIN Scanning Demo Code: Scan Many Pages into One PDF



Following C# demo code can 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<RasterImage> _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<RasterImage>();
            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;
            Bitmap[] bmps = new Bitmap[_imageList.Count];
            int index = 0;
            foreach (RasterImage image in _imageList)
            {
                bmps[index++] = image.ToBitmap();
            }
            index =0;
            REImage[] imageList = new REImage[bmps.Length];
            foreach (Bitmap bmp in bmps)
            {
                imageList[index++] = new REImage(bmp);
            }

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

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