Printdocument prints too many pages

Adagio.hpt

New member
Joined
Oct 3, 2014
Messages
4
Programming Experience
5-10
Help, I have a problem where PrintDocument is printing too many pages for no obvious reason

Unfortunately the error is a bit difficult to reproduce. I have never been able to reproduce it during testing. It is ony when users are printing documents it will fail. Usually they print documents between 2 and 5 pages. What I have seen so far is that all the times it fails, is when the users has printed exactly 3 pages

I have the following code in my print window:

C#:
PrintDocumentAdvanced p = new PrintDocumentAdvanced(tmpFileName);

p.PrinterSettings.PrinterName = "Prt-002";
p.PreparePrint();
p.Print();

The PreparePrint method is setting what pages should be print. It reads the number of pages in the tiff file:

C#:
PreparePrint()
{
    Image img = Image.FromFile(tmp);
    Guid objGuid = img.FrameDimensionsList[0];
    FrameDimension objDimension = new FrameDimension(objGuid);
    int cnt = img.GetFrameCount(objDimension);
    PrinterSettings.FromPage = 1;
    PrinterSettings.ToPage = cnt;
}

In all cases where it fails FromPage = 1 and ToPage = 3 (Could be coincidence)

When printing it will call this for each page:

C#:
private void PreparePrint(object o, PrintPageEventArgs e)
{
    Image img = Image.FromFile(tmp);
    Guid objGuid = img.FrameDimensionsList[0];
    FrameDimension objDimension = new FrameDimension(objGuid);

    // Checks if there are more pages to be printed

    Logger("Trying to print page " + lastPagePrinted + " Total pages: " + img.GetFrameCount(objDimension));

    if (img.GetFrameCount(objDimension) > lastPagePrinted + 1 && lastPagePrinted + 1 < PrinterSettings.ToPage)
    {
        e.HasMorePages = true;
    }

    Logger("Has more pages: " + e.HasMorePages);

    img.SelectActiveFrame(objDimension, lastPagePrinted);

    e.Graphics.DrawImage(img, m);
}

As you can see in the code, I have added two loggers. The result when printing a three page document:

Trying to print page 0 Total pages: 3
Has more pages: True
Trying to print page 1 Total pages: 3
Has more pages: True
Trying to print page 2 Total pages: 3
Has more pages: False
Trying to print page 3 Total pages: 3
Has more pages: False


When printing page 3 (page with index 2) it sees there are no more pages to be print and we have also reached the last page to be printed on the Settings.ToPage, but still it decides to print one more page which of course will fail as there are no more pages in the document

Any idea why?
 
Back
Top Bottom