Save scanned images to PDF

IMPORTANT:
On a 64 bit operating system, make sure your TWAIN scan driver supports 64 bit when using a target platform of AnyCpu or x64!

 

The following explains how to save scanned images into PDF documents. When using an automatic document feeder (ADF) to continuously scan images, each scanned image can be saved in separate PDF documents or saved as separate pages in single PDF document.

Example Code

Images are scanned with an automatic document feeder (ADF) and saved as separate pages of a single PDF document

Note: In order to make these examples easier to understand, only a minimum amount of code is used. For more robust scan applications, please refer to the Scan Interface properties and methods and also refer to the various scan sample projects included in the directory where the ImageKit.NET3 is installed.

[Visual Basic.NET]

'(1)Scan images with ADF, add pages in succession to a single PDF document
'Click Button1 to execute scan
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     'Suppress the manufacturer's UI and scan from the ADF (single-sided)
     ImageKit1.Scan.UserInterface = Newtone.ImageKit.ScanUserInterface.Suppress
     ImageKit1.Scan.Mode = Newtone.ImageKit.ScanMode.ADFOneSide
     'The PDF filename
     ImageKit1.File.Pdf.SaveFileName = "scanned_image.pdf"

     'Paper orientation
     ImageKit1.File.Pdf.Landscape = False
     'Paper size
     ImageKit1.File.Pdf.DocumentSize = "a4"

     ImageKit1.File.Pdf.Start()

     Dim ret As Boolean = ImageKit1.Scan.Execute()
     If Not ret Then
          MessageBox.Show("The scan failed. ErrorStatus = " + ImageKit1.ErrorStatus.ToString())
     End If

     ImageKit1.File.Pdf.End()
End Sub

'In the ImageKit control's AfterScan event, add a page and save the captured image
Private Sub ImageKit1_AfterScan(ByVal sender As Object, ByVal e As Newtone.ImageKit.AfterScanEventArgs) Handles ImageKit1.AfterScan
     ImageKit1.Image = e.Bitmap1

     'The scanned data is in BMP format which is large in size, so here we are converting it to JPEG RawData.
     ImageKit1.File.SaveImageToRawData(Newtone.ImageKit.SaveFileType.SaveJPEG, Nothing)

     ImageKit1.File.Pdf.AddPage()
     'Save as Byte() to PDF using AddImage method
     ImageKit1.File.Pdf.AddImage(ImageKit1.File.RawData, 0, 0, ImageKit1.File.Pdf.DocumentWidth, ImageKit1.File.Pdf.DocumentHeight)
     ImageKit1.File.Pdf.EndPage()
End Sub

'(2)Scan images with ADF, save each image in separate PDF files
'Click Button1 to execute scan
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     'Suppress the manufacturer's UI and scan from the ADF (single-sided)
     ImageKit1.Scan.UserInterface = Newtone.ImageKit.ScanUserInterface.Suppress
     ImageKit1.Scan.Mode = Newtone.ImageKit.ScanMode.ADFOneSide

     Dim ret As Boolean = ImageKit1.Scan.Execute()
     If Not ret Then
          MessageBox.Show("The scan failed. ErrorStatus = " + ImageKit1.ErrorStatus.ToString())
     End If
End Sub

'In the ImageKit control's AfterScan event, add a page and save the captured image
Private Sub ImageKit1_AfterScan(ByVal sender As Object, ByVal e As Newtone.ImageKit.AfterScanEventArgs) Handles ImageKit1.AfterScan
     ImageKit1.Image = e.Bitmap1

     'File name to save PDF (In this example, we add a number to the end of the filename so that the previous PDF will not be overwritten.)
     ImageKit1.File.Pdf.SaveFileName = "scanned_image" + e.ImageNumber.ToString() + ".pdf"
     'Paper orientation
     ImageKit1.File.Pdf.Landscape = False
     'Paper size
     ImageKit1.File.Pdf.DocumentSize = "a4"

     ImageKit1.File.Pdf.Start()
     ImageKit1.File.Pdf.AddPage()
     'Embed the captured image on the entire page
     ImageKit1.File.Pdf.AddImage(ImageKit1.File.RawData, 0, 0, ImageKit1.File.Pdf.DocumentWidth, ImageKit1.File.Pdf.DocumentHeight)
     ImageKit1.File.Pdf.EndPage()
     ImageKit1.File.Pdf.End()
End Sub

[Visual C#.NET]

//(1)Scan images with ADF, add pages in succession to a single PDF document
//Click Button1 to execute scan
private void button1_Click(object sender, EventArgs e)
{
     //Suppress the manufacturer's UI and scan from the ADF (single-sided)
     ImageKit1.Scan.UserInterface = Newtone.ImageKit.ScanUserInterface.Suppress;
     ImageKit1.Scan.Mode = Newtone.ImageKit.ScanMode.ADFOneSide;

     //PDF Filename
     ImageKit1.File.Pdf.SaveFileName = "scanned_image.pdf";

     // Paper orientation
     ImageKit1.File.Pdf.Landscape = false;
    //Paper size
     ImageKit1.File.Pdf.DocumentSize = "a4";

     ImageKit1.File.Pdf.Start();

     bool ret = ImageKit1.Scan.Execute();
     if (!ret)
     {
          MessageBox.Show("The scan failed. ErrorStatus = " + ImageKit1.ErrorStatus.ToString());
     }
     ImageKit1.File.Pdf.End();
}

//In the ImageKit control's AfterScan event, add a page and save the captured image
private void imageKit1_AfterScan(object sender, Newtone.ImageKit.AfterScanEventArgs e)
{
     ImageKit1.Image = e.Bitmap1;

     //The scanned data is in BMP format which is large in size, so here we are converting it to JPEG RawData.
     ImageKit1.File.SaveImageToRawData(Newtone.ImageKit.SaveFileType.SaveJPEG, null);

     ImageKit1.File.Pdf.AddPage();
     //Save as Byte[] to PDF using AddImage method
     ImageKit1.File.Pdf.AddImage(ImageKit1.File.RawData, 0, 0, ImageKit1.File.Pdf.DocumentWidth, ImageKit1.File.Pdf.DocumentHeight);
     ImageKit1.File.Pdf.EndPage();
}

 

//(2)Scan images with ADF, save each image in separate PDF files
//Click Button1 to execute scan
private void button1_Click(object sender, EventArgs e)
{
     //Suppress the manufacturer's UI and scan from the ADF (single-sided)
     ImageKit1.Scan.UserInterface = Newtone.ImageKit.ScanUserInterface.Suppress;
     ImageKit1.Scan.Mode = Newtone.ImageKit.ScanMode.ADFOneSide;

     bool ret = ImageKit1.Scan.Execute();
     if (!ret)
     {
          MessageBox.Show("The scan failed. ErrorStauts = " + ImageKit1.ErrorStatus.ToString());
     }
}

//In the ImageKit control's AfterScan event, add a page and save the captured image
private void imageKit1_AfterScan(object sender, Newtone.ImageKit.AfterScanEventArgs e)
{
     ImageKit1.Image = e.Bitmap1;

     //File name to save PDF (In this example, we add a number to the end of the filename so that the previous PDF will not be overwritten.)
     ImageKit1.File.Pdf.SaveFileName = "scanned_image" + e.ImageNumber.ToString() + ".pdf"
     //Paper orientation
     ImageKit1.File.Pdf.Landscape = false;
     //Paper size
     ImageKit1.File.Pdf.DocumentSize = "a4";

     ImageKit1.File.Pdf.Start();
     ImageKit1.File.Pdf.AddPage();
     //Embed the captured image on the entire page
     ImageKit1.File.Pdf.AddImage(ImageKit1.File.RawData, 0, 0, ImageKit1.File.Pdf.DocumentWidth, ImageKit1.File.Pdf.DocumentHeight);
     ImageKit1.File.Pdf.EndPage();
     ImageKit1.File.Pdf.End();
}

Relevant Properties

ImageKit.Scan.UserInterface
ImageKit.Scan.Mode
ImageKit.File.PDF.SaveFileName
ImageKit.File.PDF.Landscape
ImageKit.File.PDF.DocumentSize

Relevant Methods

ImageKit.Scan.Execute
ImageKit.File.PDF.Start
ImageKit.File.PDF.End
ImageKit.File.PDF.AddPage
ImageKit.File.PDF.AddImage
ImageKit.File.PDF.EndPage

Relevant Events

ImageKit.AfterScan

 

For other scan-related settings, see the other scan properties below and refer to the scan sample projects
ImageKit.Scan Members

The ImageKit.NET3 is created by Newtone Corporation