Reputation:
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.
Upvotes: 0
Views: 1539
Reputation: 737
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.
Upvotes: 1