This is the best I've come up with:
public static int GetPageCount( PrintDocument printDocument )
{
printDocument.PrinterSettings.PrintFileName = Path.GetTempFileName();
printDocument.PrinterSettings.PrintToFile = true;
int count = 0;
printDocument.PrintController = new StandardPrintController();
printDocument.PrintPage += (sender, e) => count++;
printDocument.Print();
File.Delete( printDocument.PrinterSettings.PrintFileName );
return count;
}
Is there a better way to do this? (This is actually quite slow)
So the final solution would be:
public static int GetPageCount(PrintDocument printDocument)
{
int count = 0;
printDocument.PrintController = new PreviewPrintController();
printDocument.PrintPage += (sender, e) => count++;
printDocument.Print();
return count;
}
Declare the PrintController as a Printing.PreviewPrintController
.
This way, you're only printing to memory, not to a file.
I use this in a VB.NET project, and it works perfectly!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With