TWAIN Working Group

Newsletter Signup
Donate
Help keep TWAIN free
  • About TWAIN
    • What’s New?
    • News
    • Events
    • Membership
    • Consider a Donation
    • Contact Us
  • Why TWAIN?
  • Developers
    • Driver Developer
    • Application Developer
    • TWAIN Features
    • Specification & Tools
    • Self Certification Process
  • Support Forums
  • Scanner End-User
  • Find Certified Drivers
    • Facebook
    • LinkedIn
    • Vimeo

Twain error 42: Actual page count != Expected page count

Forums › TWAIN Classic › Twain error 42: Actual page count != Expected page count

  • This topic has 4 replies, 3 voices, and was last updated 4 years, 10 months ago by evanpan.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • April 16, 2013 at 12:34 am #22839 Reply
    jeet
    Participant
    • Topics - 1
    • Replies - 1
    • Total Posts - 2

    We use the EZTwain 3.41.0.9 library to implement our scanning UI and logic. We have a batch scanning feature where one is able to scan multiple batches of pages into one PDF document. So the general process is: drop a set of pages into the scanner’s document feeder, start the scan, be prompted once all the pages are scanned whether to continue scanning, click Yes, get prompt to click OK once feeder is loaded again, put in the 2nd set of pages into the document feeder, click OK and continue scanning, rinse and repeat for 3rd batch etc. Once “continue scanning?” is answered as No, the loop exits and finalizes the multi-page pdf generated from the scans.

    One of our clients is using Fujistu 6130 scanners and is getting a weird twain error after 2-3 sets of pages have been processed as described above: “Twain Error 42: Actual page count (13) != Expected page count (26)” or “Twain Error 42: Actual page count (65) != Expected page count (80)” etc.

    Below is a code sample that basically describes the process flow/loop I am using. There is an outer “while” loop with an inner “do” loop. The “do” loop scans a set of pages, then the outer “while” loop asks if user wants to continue batch scanning, and if so, it repeats the “do” loop for the next set of pages put into the scanner.

    I understand the calls are specific to EZTwain but I was wondering if a ‘twain wizard’ out there could possibly see what could be causing the scanner to throw the “twain 42: actual page count != expected page count” error after a few passes through the outer “while” loop. After a few executions of the inner “do” loop, the call to EZTwain.LastErrorCode() after the “do” loop returns error 42.

    Note the full-duplex setting is enabled on the scanner, as well as the logic to “reinitialize” the scanner settings between each execution of the “do” loop if user chooses to continue scanning (basically repeat the same set of commands as the start to set the scanner properties) since “certain TWAIN drivers (specifically Fujuitsu 6230 models) reset some of the feeding options between acquire calls”.

    Somewhere I had also read “If you want to follow the TWAIN Capability Ordering documentation you have to set CAP_XFERCOUNT twice. Once before CAP_DUPLEXENABLED to any acceptable value (e.g. 2) and once in its proper place near the end to the actual value you want.” I wonder if I need to account for that or if the order of calls is somehow causing the scanner to “lose it’s place” in terms of the number of pages coming through. Note the EZTwain.SetXferCount(-1) call is only at the start and not within the “while” loop for 2nd and subsequent batches.

    I have searched everywhere for any “actual page count doesn’t match expected page count” twain errors but have not found anything. Any hints or suggestions would be greating appreciated, if anybody has ever encountered this issue.

    EZTwain.SetHideUI(true);
    EZTwain.SelectFeeder(true);
    EZTwain.SetAutoFeed(true);
    EZTwain.EnableDuplex(true);
    EZTwain.SetPixelType(TWPT_GRAY);
    EZTwain.SetBitDepth(1);
    EZTwain.SetResolution(300);
    EZTwain.SetAutoCrop(0);
    EZTwain.SetAutoDeskew(0);
    EZTwain.SetPaperSize(scannerOptionsControl.SelectedPaperSize);
    EZTwain.SetLightPath(false);

    EZTwain.SetXferCount(-1);
    EZTwain.SetAutoScan(true);
    EZTwain.SetMultiTransfer(true);
    EZTwain.SetFileAppendFlag(false);

    Boolean continueScanning = true;
    while (continueScanning)
    {
    EZTwain.BeginMultipageFile(tempFile);

    do
    {
    hdib = EZTwain.Acquire(this.Handle);
    if (hdib == IntPtr.Zero)
    {
    break;
    }
    EZTwain.DibWritePage(hdib);
    EZTwain.DIB_Free(hdib);

    } while (EZTwain.State() >= EZTwain.TWAIN_TRANSFER_READY);

    EZTwain.CloseSource();
    EZTwain.EndMultipageFile();

    if (EZTwain.LastErrorCode() != 0)
    {
    MessageBoxHelper.ShowMessage(“The application is unable to complete the scan. Twain Error: ” + EZTwain.LastErrorCode() + ” – ” + EZTwain.LastErrorText(), this);
    return;
    }

    if (MessageBox.Show(“Do you have more pages to scan for this document?”) == DialogResult.Yes)
    {
    // Pause until the user is ready to continue scanning. If the user presses Cancel, then finalize scanning for this document.
    if (MessageBox.Show(“Press OK when you are ready to continue scanning.”) == DialogResult.Cancel)
    {
    continueScanning = false;
    }
    else
    {
    // Set the continue flag to true
    continueScanning = true;

    // Re-initialize scanner between acquire calls. Certain TWAIN drivers (specifically Fujuitsu 6230 models)
    // reset some of the feeding options between acquire calls. This causes subsequent scans to be acquired from
    // the flatbed instead of the ADF (which results in an error). The initialize scanner call will force the same
    // settings that were used in the first scan.
    EZTwain.SetHideUI(true);
    EZTwain.SelectFeeder(true);
    EZTwain.SetAutoFeed(true);
    EZTwain.EnableDuplex(true);
    EZTwain.SetPixelType(TWPT_GRAY);
    EZTwain.SetBitDepth(1);
    EZTwain.SetResolution(300);
    EZTwain.SetAutoCrop(0);
    EZTwain.SetAutoDeskew(0);
    EZTwain.SetPaperSize(scannerOptionsControl.SelectedPaperSize);
    EZTwain.SetLightPath(false);

    // Tell the twain driver to append the next batch of pages to the previously
    // opened temp file. If the FileAppendFlag is false, then the first batch of
    // pages is overwritten.
    EZTwain.SetFileAppendFlag(true);
    }
    }
    else
    {
    // Finalize scanning
    continueScanning = false;
    }
    }

    Thanks!

    April 16, 2013 at 8:17 pm #25867 Reply
    spike
    Participant
    • Topics - 10
    • Replies - 139
    • Total Posts - 149

    Hi Jeet – that’s not a TWAIN error, it’s coming from EZTwain.
    That error might be reported in your call to EZTwain.EndMultipageFile(), because for some reason EZTwain finds that the PDF file it just closed has e.g. 13 pages when EZTwain expects it to have 26.

    I don’t see any obvious problem with your code, so…
    Would it be possible for you or your client to reproduce this problem with DosadiLog running, and to post here the resulting log?
    DosadiLog instructions are here, with a download link:
    http://www.eztwain.com/dosadilog.htm
    (The installer is signed by Kofax, who now owns it as part of the EZTwain toolkit.)
    Note 1: You should start DosadiLog before starting your application.
    Note 2: It is not necessary to redact the log, sensitive information is omitted or ‘scrambled’ in the log.

    April 18, 2013 at 8:33 pm #25868 Reply
    jeet
    Participant
    • Topics - 1
    • Replies - 1
    • Total Posts - 2

    Hi Spike! Hope everything is going well, good to talk with you again!

    I installed the FreeImage sample data source app on my machine and tested our software with DosadiLog running, attached is the resulting log file.

    Since the virtual data source only acquires one page at a time, I had to do about 10 passes through our batch processing loop before the error “actual page count (10) != expected page count (11)” popped up, as shown at the end of the log. I wonder if the fact that on the very first pass, EZTwain.SetFileAppendFlag(false) sets the ‘fileappend’ flag to false but on 2nd and subsequent passes, it is set to true to allow only one pdf file to be appended to: EZTwain.SetFileAppendFlag(true)? So perhaps the flag should bet set to true from the very begining, even for the very first pass? I will modify my logic to test that out when I get a chance today or tomorrow, if nothing else seems obviously wrong from the log.

    I truly appreciate your help with this, Spike, thanks again.

    April 19, 2013 at 2:32 pm #25869 Reply
    spike
    Participant
    • Topics - 10
    • Replies - 139
    • Total Posts - 149

    Hi Jeet – thanks for the log. I don’t see anything going obviously wrong in there!
    But I also have an idea for something to try:
    You don’t need to end & restart the PDF file inside your loop: Move the BeginMultipageFile and EndMultipageFile calls outside your while (continueScanning) loop.
    You’ll get a smaller and ‘internally cleaner’ PDF, and I bet it will eliminate this error, even though I still don’t know exactly what is causing the error.

    One problem is that in case of an error, you could end up with a 0-page PDF. If you care about that, you could just delete the PDF when done. (After calling EndMultipageFile first! ALWAYS call EndMultipageFile, to match any successful call to BeginMultipageFile.)

    March 2, 2016 at 11:01 am #25870 Reply
    evanpan
    Participant
    • Topics - 0
    • Replies - 7
    • Total Posts - 7

    Hi, spike.
    Thanks for sharing your problem. As for me, I have seldom tried to deal with that kind of problem before. Have you ever worked it out? I wonder whether you have any exprience about
    pdf extracting process. Because there is something wrong with my pdf reader. I need convert pdf into text or other formats. Any suggestion will be appreciated. Thanks in advance.

    Best regards,
    Pan

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Twain error 42: Actual page count != Expected page count
Your information:




Quick Links

Service Providers
TWAIN Support Forums
Membership
Contact Us
Privacy Policy

Newsletter Signup

TWAIN Working Group Family

TWAIN Working Group
TWAIN Direct®
TWAIN Resources
TWAIN Certified Drivers
PDF/raster

  • Facebook
  • GitHub
  • LinkedIn
  • Vimeo

Recent Topics

  • EPSON V600 TWAIN and WIA on Windows 10
  • When and how to use WaitForEvents command ?
  • Problem enumerating list of installed scanners in windows server 2012
  • Failed to create TWAIN progress! Error code is 1260.
  • To get the list of scanners from javascript client side (browser)
  • Quarterly Newsletter
  • TWAIN Working Group Membership
  • Logo Usage
  • TWAIN License
  • Contact Us
Privacy Policy • Privacy Tools • Copyright © 2021 TWAIN Working Group • by iHwy, LLC • Log in

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.