Page 1 of 1

Scanning multiple pages

Posted: Tue Jan 12, 2010 12:56 am
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.");
}
}
}

Re: Scanning multiple pages

Posted: Tue Jan 12, 2010 9:17 am
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

Re: Scanning multiple pages

Posted: Tue Jan 12, 2010 9:27 am
by BillG
Do I need to set the MaxImages property to the number of pages being scanned?

Re: Scanning multiple pages

Posted: Tue Jan 12, 2010 11:52 am
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.

Re: Scanning multiple pages

Posted: Wed Jan 13, 2010 12:14 am
by BillG
How do I change the above change to scan multiple pages and save them in the same pdf file?

Re: Scanning multiple pages

Posted: Wed Jan 13, 2010 12:27 am
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;

Re: Scanning multiple pages

Posted: Wed Jan 13, 2010 1:21 am
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;
}

Re: Scanning multiple pages

Posted: Wed Jan 13, 2010 11:35 am
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