The PrintDocument class sits at the heart of Visual Basic’s printing process. The program creates an instance of this class and installs event handlers to catch its events. When the object must perform printing-related tasks, it raises events to ask the program for help.
The PrintDocument object raises four key events:
- BeginPrint — The PrintDocument raises its BeginPrint event when it is about to start printing. The program can initialize data structures, load data, connect to databases, and perform any other chores it must do to get ready to print.
- QueryPageSettings — Before it prints a page, the PrintDocument object raises its QueryPageSettings event. A program can catch this event and modify the document’s margins for the page that it is about to print.
- PrintPage — The PrintDocument object raises its PrintPage event to generate a page. The program must catch this event and use the Graphics object provided by the event handler’s parameters to generate output. When it is finished, the event handler should set the value e.HasMorePages to True or False to tell the PrintDocument whether there are more pages to generate after this one.
- EndPrint — When it has finished printing, the PrintDocument object raises its EndPrint event. The program can catch this event to clean up any resources it used while printing. It can free data structures, close data files and database connections, and perform any other necessary cleanup chores.
Having created a PrintDocument object ...