Scanning multiple pages

Questions, comments and suggestions concerning VintaSoft Twain .NET SDK.

Moderator: Alex

Post Reply
BillG
Posts: 47
Joined: Fri Oct 10, 2008 3:52 pm

Scanning multiple pages

Post by BillG »

Up to this point, my client is using the software to scan single membership cards and everything works fine. Now they want to scan multiple page documents and put the multi page document into one pdf file. How do I set up my code for this so that I capture multiple pages before saving the image. Here is my present code.

try
{
vsTwain1.StartDevice();
vsTwain1.ShowUI = false;
if (vsTwain1.SelectSource())
{
vsTwain1.DisableAfterAcquire = true;
vsTwain1.MaxImages = 1;
vsTwain1.AutoCleanBuffer = true;
vsTwain1.OpenDataSource();
vsTwain1.PixelType = PixelType.BW;
if (vsTwain1.FeederPresent)
{
vsTwain1.FeederEnabled = true;
vsTwain1.AutoFeed = true;
vsTwain1.XferCount = 1;
vsTwain1.PdfMultiPage = false;
if (vsTwain1.FeederLoaded)
{
vsTwain1.ShowIndicators = true;
if (vsTwain1.AcquireModal())
{
try
{
this.Cursor = Cursors.WaitCursor;
MemoryStream mem = new MemoryStream();
mem = vsTwain1.GetImageAsStream(0, ImageFileFormat.PDF);
string dirPath = "\\\\192.168.254.5\\RelatedDocuments\\EmployerDocuments\\" + myController.CurrentEmployer.Office + "\\";
string dateString = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
string filePath = myController.CurrentEmployer.EmployerId + dateString + ".pdf";
myController.SaveScannedRecord(Int32.Parse(ScanItemTypeComboBox.SelectedValue.ToString()), filePath);
vsTwain1.SaveImage(0, dirPath + filePath);
}
catch (TwainException ex)
{
MessageBox.Show(ex.Message);
}
}
vsTwain1.StopDevice();
MessageBox.Show("Scan completed.");
}
}
}
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Scanning multiple pages

Post by Alex »

Hello Bill,

Your code has logical mistake - you should use "while" statement instead of the "if" when you work with AcquireModal method. Here is correct code:

Code: Select all

try
{
  vsTwain1.StartDevice();
  vsTwain1.ShowUI = false;
  if (vsTwain1.SelectSource())
  {
    vsTwain1.DisableAfterAcquire = true;
    vsTwain1.MaxImages = 1;
    vsTwain1.AutoCleanBuffer = true;
    vsTwain1.OpenDataSource();
    vsTwain1.PixelType = PixelType.BW;
    if (vsTwain1.FeederPresent)
    {
       vsTwain1.FeederEnabled = true;
       vsTwain1.AutoFeed = true;
       vsTwain1.XferCount = 1;
       vsTwain1.PdfMultiPage = false;
       if (vsTwain1.FeederLoaded)
       {
          vsTwain1.ShowIndicators = true;
          //if (vsTwain1.AcquireModal()) - wrong code
          while (vsTwain1.AcquireModal()) - correct code
          {
             try
             {
                this.Cursor = Cursors.WaitCursor;
                MemoryStream mem = new MemoryStream();
                mem = vsTwain1.GetImageAsStream(0, ImageFileFormat.PDF);
                string dirPath = "\\\\192.168.254.5\\RelatedDocuments\\EmployerDocuments\\" + myController.CurrentEmployer.Office + "\\";
                string dateString = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
                string filePath = myController.CurrentEmployer.EmployerId + dateString + ".pdf";
                myController.SaveScannedRecord(Int32.Parse(ScanItemTypeComboBox.SelectedValue.ToString()), filePath);
                vsTwain1.SaveImage(0, dirPath + filePath);
             }
             catch (TwainException ex)
             {
                MessageBox.Show(ex.Message);
             }
        }
        vsTwain1.StopDevice();
        MessageBox.Show("Scan completed.");
    }
  }
}
Best regards, Alexander
BillG
Posts: 47
Joined: Fri Oct 10, 2008 3:52 pm

Re: Scanning multiple pages

Post by BillG »

Do I need to set the MaxImages property to the number of pages being scanned?
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Scanning multiple pages

Post by Alex »

Do I need to set the MaxImages property to the number of pages being scanned?
Yes, you should set the MaxImages property if you want to store pages in the internal buffer of the library but make sure that you have enough memory. The best way is to store images in the MemoryStream or file.
BillG
Posts: 47
Joined: Fri Oct 10, 2008 3:52 pm

Re: Scanning multiple pages

Post by BillG »

How do I change the above change to scan multiple pages and save them in the same pdf file?
BillG
Posts: 47
Joined: Fri Oct 10, 2008 3:52 pm

Re: Scanning multiple pages

Post by BillG »

I tried this but got a attempted to divide by zero error

vsTwain1.StartDevice();
vsTwain1.ShowUI = false;
if (vsTwain1.SelectSource())
{
vsTwain1.DisableAfterAcquire = true;
vsTwain1.MaxImages = Int32.Parse(NumberOfPagesTextBox.Value.ToString());
vsTwain1.AutoCleanBuffer = true;
vsTwain1.OpenDataSource();
vsTwain1.PixelType = PixelType.BW;
if (vsTwain1.FeederPresent)
{
vsTwain1.FeederEnabled = true;
vsTwain1.AutoFeed = true;
vsTwain1.XferCount = 1;
vsTwain1.PdfMultiPage = false;
if (vsTwain1.FeederLoaded)
{
vsTwain1.ShowIndicators = true;
int pageCount = 0;
this.Cursor = Cursors.WaitCursor;
MemoryStream mem = new MemoryStream();
while (vsTwain1.AcquireModal())
{
try
{
mem = vsTwain1.GetImageAsStream(pageCount, ImageFileFormat.PDF);
pageCount++;
}
catch (TwainException ex)
{
if (vsTwain1.ErrorCode != Vintasoft.Twain.ErrorCode.None)
MessageBox.Show(vsTwain1.ErrorString);
else
MessageBox.Show(ex.Message);
}
}
string dirPath = "q:\\JobSiteDocuments\\" + myController.CurrentJobSite.Office + "\\";
string dateString = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
RelatedDocumentType rdType = (RelatedDocumentType)ScanItemTypeComboBox.SelectedItem;
string docDesc = rdType.DocumentTypeDesc;
string filePath = myController.CurrentJobSite.JobSiteId + docDesc + dateString + ".pdf";
Int32 numOfPages = Int32.Parse(NumberOfPagesTextBox.Value.ToString());
myController.SaveScannedRecord(Int32.Parse(rdType.DocumentTypeId.ToString()), filePath, DescriptionTextBox.Text.ToString(), numOfPages);
vsTwain1.SaveImage(0, dirPath + filePath);
vsTwain1.StopDevice();
MessageBox.Show("Scan completed.");
}
}
}
DescriptionTextBox.Text = string.Empty;
NumberOfPagesTextBox.Value = 1;
PopulateDocumentsGrid();
this.Cursor = Cursors.Arrow;
BillG
Posts: 47
Joined: Fri Oct 10, 2008 3:52 pm

Re: Scanning multiple pages

Post by BillG »

Well I have it now scanning multiple pages and creating only one pdf file but it always only saves the last page.

vsTwain1.StartDevice();
vsTwain1.ShowUI = false;
if (vsTwain1.SelectSource())
{
vsTwain1.DisableAfterAcquire = true;
vsTwain1.MaxImages = Int32.Parse(NumberOfPagesTextBox.Value.ToString());
vsTwain1.AutoCleanBuffer = true;
vsTwain1.OpenDataSource();
vsTwain1.PixelType = PixelType.BW;
if (vsTwain1.FeederPresent)
{
vsTwain1.FeederEnabled = true;
vsTwain1.AutoFeed = true;
vsTwain1.XferCount = 1;
vsTwain1.PdfMultiPage = false;
if (vsTwain1.FeederLoaded)
{
vsTwain1.ShowIndicators = true;
this.Cursor = Cursors.WaitCursor;
MemoryStream mem = new MemoryStream();
bool firstImage = true;
int counter = 0;
while (vsTwain1.AcquireModal())
{
try
{
if (firstImage)
{
mem = vsTwain1.GetImageAsStream(0, ImageFileFormat.PDF);
firstImage = false;
counter++;
}
else
{
vsTwain1.SaveImageToStream(counter, mem, ImageFileFormat.PDF);
counter++;
}
}
catch (TwainException ex)
{
if (vsTwain1.ErrorCode != Vintasoft.Twain.ErrorCode.None)
MessageBox.Show(vsTwain1.ErrorString);
else
MessageBox.Show(ex.Message);
}
}
string dirPath = "q:\\JobSiteDocuments\\" + myController.CurrentJobSite.Office + "\\";
string dateString = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
RelatedDocumentType rdType = (RelatedDocumentType)ScanItemTypeComboBox.SelectedItem;
string docDesc = rdType.DocumentTypeDesc;
string filePath = myController.CurrentJobSite.JobSiteId + docDesc + dateString + ".pdf";
Int32 numOfPages = Int32.Parse(NumberOfPagesTextBox.Value.ToString());
myController.SaveScannedRecord(Int32.Parse(rdType.DocumentTypeId.ToString()), filePath, DescriptionTextBox.Text.ToString(), numOfPages);
vsTwain1.SaveImage(vsTwain1.MaxImages-1, dirPath + filePath);
vsTwain1.StopDevice();
MessageBox.Show("Scan completed.");
}
}
}
DescriptionTextBox.Text = string.Empty;
NumberOfPagesTextBox.Value = 1;
PopulateDocumentsGrid();
this.Cursor = Cursors.Arrow;
}
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: Scanning multiple pages

Post by Alex »

Hello Bill,
Well I have it now scanning multiple pages and creating only one pdf file but it always only saves the last page.
What scanner do you have? Maybe Brother?

Please read this topic:
viewtopic.php?f=19&t=3

Best regards, Alexander
Post Reply