Posted by: Cirilo Meggiolaro | 10/20/2008

Tip of the day #6 – Windows Forms Printing Events

Print a custom document with .NET is quite simple and straightforward.

The start point to print is to create a PrintDocument object, add event handlers that fit your application needs and invoke the method Print().

The following event handlers are available for the PrintDocument object:

1. BeginPrint
The BeginPrint event is raised just once and before the first page prints.
Use the event to preset print level properties.

2. EndPrint
The EndPrint event is raised just once and after the last page has printed.
Use the event to release resources, notify the user that the print has finished or any other after print needs.

3. PrintPage
The PrintPage event is raised for each page and while the PrintDocument object has pages to be printed.
This is where the magic happens. Use the event to set the content that will be printed in the current page like text, images and so on.

For each printed page you’ll have to check if more pages must be printed and set the boolean property HasMorePages from the PrintEventArgs parameter to allow the event be raised again or finish the print process.

4. QueryPageSettings
The QueryPageSettings event is raised before each page prints.
Use the event to preset page level properties.


Leave a comment

Categories