views:

301

answers:

2

I am writing an app to print formatted data using Visual Studio 2008/C#. I have formatted the data in the fashion that I want it to display. I am using two Print Documents and event handlers, because the first page of the report carries formatting requirements that differs from pages 2 through N.

Print Preview shows me properly formatted data for all pages that I try to print. Nevertheless, pages 2 through N will not actually print.

I've stepped through my code and the data is being passed correctly to the event handler. This is the block of code that calls the second print document's event handler. What am I doing wrong?

         // First page print limit has been reached. Do we
        //  still have unprinted items in the arraylist? Call the second 
        //  print handler event and print those items.
        if (((alItemsToPrint.Count) - iItemPrintedCount) > 0)
        {
            // Getting a look at my formating
            PrintPreviewDialog printPreview2 = new PrintPreviewDialog();
            printPreview2.Document = ItemsPrintDocument;
            printPreview2.ShowDialog();
            printPreview2.Dispose();                               

            // Print item overflow pages
            ItemsPrintDocument.Print();

            // Release the resources consumed by this print document
            ItemsPrintDocument.Dispose();
        }

Thanks for your time, everyone.

A: 

Thanks, Henk, but that's still not working.

Alright, I've discovered a few things since yesterday. The problem doesn't appear to be in the code I listed above. Instead, printing pages 2 - n seems to be stymied by this bit of code:

        int iNewPage = 0;
        iNewPage = (alItemsToPrint.Count) - iItemPrintedCount;

        // We've reached our page limit for printed items, Do we
        //  still have unprinted items in the arraylist? Re-call this 
        //  print event handler and print those items.
        if (iNewPage > 0)
        {
            EventArgs.HasMorePages = true;
        }
        else
        {
            EventArgs.HasMorePages = false;
        }

This bit of code is located at the bottom of the second print document's event handler. If I comment it out, I get the first and Nth pages of my report to print. They are properly formatted and display the correct information.

If I leave this code in, I get the header information for the Nth page, but none of the information provided by the data-gathering loop gets printed.

Any suggestions?

GFalcon
I discovered the problem, although I have no idea WHY it was a problem. it turns out that the print preview, even after the call to Dispose was commented out, was somehow flushing some of the data being sent to the printer.So, If I ran the app with the code listed at the top of the page, the actual print job would be incomplete. Once I removed the preview, the print job completed correctly.So now I am left wondering why this happens? Is it an error in how I called the print preview dialog box?
GFalcon
A: 

To Print a Document, you use:

PrintDocument.Print

When Preview, You assign The PrintDocument to PrintPreviewDialog

printPreview2.Document = ItemsPrintDocument;

When You show PrintPreviewDialog, it replaces the PrintDocument' PrintController to PreviewPrintController and call PrintDocument.Print.

This action generates a List of images (metafiles) one on each page.

Next, it restore the original PrintController on PrintDocument and show images.

When You press the PrintButton on PrintPreviewDialog, it calls PrintDocument.Print with original PrintController.

Note that for correct behavior you can use BeginPrint' PrintDocument event to initialize vars to new PrintDocument.Print.

If you use PrintPreviewDialog, you do´nt need call PrintDocument.Print.

x77