Reputation: 2014
I currently have a function that is able to printout a DataGridView onto a page using System.Drawing.Printing.PrintDocument - the printing utility runs through my print page function (PrintDoc_PagePrint) until it runs out of rows (where it will set HasMorePages to false.)
I'm trying to work out the total number of pages before I print so I can put "page x of y" at the bottom of each page. One way is to work out how many rows fit on each page and work out how many pages there are based on how many rows there are in total, but this doesn't seem very versatile as it relies on each row being the same height, which depending on how it's programmed, may not always be the case.
The way I want to do it is to do a "phantom" print - or basically print it to a null printer in the background without the user knowing. When it does this first print, it can increment a global variable TotalPages each time it runs the print function, then once the phantom print is done stop TotalPages being incremented next time it is printed (presumably just by setting a bool once the phantom print is done.) This would be more versatile, and would work for data grids with different row heights, or any other type of data I wanted to print.
My question is - is there any way to run a sample print in the background? This is done after the user has selected page size and orientation etc, so we do know those essential details, but just before the print preview dialogue is displayed.
Here's some code I have... it sort of works, but for some reason it doesn't work all the time!
// Phantom print to determine number of pages. Writes to TotalPages var.
// The next print won't write to TotalPages when FirstPreviewDone is set to true.
var printEventArgs = new PrintEventArgs();
// Create a graphics object of the page size to "print" to.
int x = 0;
int y = 0;
int width = printDoc.DefaultPageSettings.PaperSize.Width;
int height = printDoc.DefaultPageSettings.PaperSize.Height;
Rectangle marginBoundsRectangle = new Rectangle(x, y, width, height);
Rectangle pageBoundsRectangle = new Rectangle(0, 0, printDoc.DefaultPageSettings.PaperSize.Width, printDoc.DefaultPageSettings.PaperSize.Height);
Bitmap b = new Bitmap(width, height);
// Swap everything if it's in landscape.
if (printDoc.DefaultPageSettings.Landscape)
{
marginBoundsRectangle = new Rectangle(y, x, height, width);
pageBoundsRectangle = new Rectangle(0, 0, printDoc.DefaultPageSettings.PaperSize.Height, printDoc.DefaultPageSettings.PaperSize.Width);
b = new Bitmap(height, width);
}
Graphics graphics = Graphics.FromImage(b);
var printPageEventArgs = new PrintPageEventArgs(graphics, marginBoundsRectangle, pageBoundsRectangle, printDoc.DefaultPageSettings);
printPageEventArgs.HasMorePages = true;
PrintDoc_BeginPrint(null, printEventArgs);
while (printPageEventArgs.HasMorePages && !printPageEventArgs.Cancel)
{
try
{
PrintDoc_PrintPage(null, printPageEventArgs);
}
catch (Exception ex)
{
MessageBoxEx.Show(ex.Message, "Error printing - Check logs", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
Upvotes: 3
Views: 3714
Reputation: 1
int iPageCount = rptDocument.FormatEngine.GetLastPageNumber(new ReportPageRequestContext());
Upvotes: -1
Reputation: 3388
This is already answered in the below question...
Is there a better way to get the page count from a PrintDocument than this?
Hope this helps...
Upvotes: 2