As this is a
System.Windows.Forms
application, normally you need to use the class
System.Drawing.Printing.PrintDocument
:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx[
^].
Now, look at the small code sample at the end. It should how to print, and, in particular, how to print a page by adding a handler to the invocation list of the event
System.Drawing.Printing.PrintDocument.PrintPage
, see
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printpageeventargs.aspx[
^].
Add some counter in the implementation of the handler; and you are done:
void PrintADocument(SomeData someData ) {
PrintDocument pd = new PrintDocument();
uint pageCount = 0;
pd.PrintPage += (sender, eventArgs) {
System.Drawing.Graphics graphics = eventArgs.Graphics;
RenderThePageUsingTheInstanceOfGraphics(graphics, someData);
pageCount++;
};
pd.Print();
}
In this code, I also used an interesting feature, related to
anonymous methods and the use of the stack variable
pageCount
, called
closure. This is interesting enough to learn.
http://en.wikipedia.org/wiki/Closure_%28computer_science%29[
^],
http://en.wikipedia.org/wiki/Anonymous_method[
^],
http://en.wikipedia.org/wiki/Anonymous_method#C.23_lambda_expressions[
^].
—SA