Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to get the page count from a PrintDocument than this?

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)

like image 310
Jonathan Mitchem Avatar asked Jun 29 '10 01:06

Jonathan Mitchem


2 Answers

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;
}
like image 153
TzOk Avatar answered Nov 09 '22 18:11

TzOk


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!

like image 42
Erik van der Velden Avatar answered Nov 09 '22 19:11

Erik van der Velden