[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Provide printing and print previews with advanced features for Windows Forms applications in C#

printing

The example Provide print previews and printing for Windows Forms applications in C# shows how to create a basic printout by handling a PrintDocument object's PrintPage event. This example shows how to use two other PrintDocument events: BeginPrint and QueryPageSettings.

BeginPrint

BeginPrint fires just before a printout starts. You can use it to prepare for the printout by opening files, connecting to databases, or whatever.

This example uses BeginPrint to initialize the class-level variables NextPageNum that keeps track of the next page to print. The previous example reset this variable to 0 after it printed the last page of a printout so it would be ready for the next printout. This version is a bit more intuitive because it sets NextPage = 0 before printing instead of afterward.

private int NextPageNum = 0; // Get ready to print. private void pdocShapes_BeginPrint(object sender, PrintEventArgs e) { // Start with page 0. NextPageNum = 0; }

QueryPageSettings

The QueryPageSettings event fires just before each page is generated. You can use this event to prepare to print the next page. In particular, you can set the page's margins.

This example, assumes you are printing a booklet and adds a gutter to each page. A gutter is an extra bit of space next to the margin to allow for the booklet's staples or stitching for a book's binding. This example adds a gutter to the left of even-numbered pages and to the right of odd-numbered pages (remember, numbering starts with 0).

// Prepare to print the next page. private void pdocShapes_QueryPageSettings(object sender, QueryPageSettingsEventArgs e) { const int GUTTER = 100; // Even numbered pages have a big margin on the left. if (NextPageNum == 0) { // The first page. Increase the left margin. e.PageSettings.Margins.Left += GUTTER; } else if (NextPageNum % 2 == 0) { // An even page. Increase the left margin // and decrease the right margin. e.PageSettings.Margins.Left += GUTTER; e.PageSettings.Margins.Right -= GUTTER; } else { // An odd page. Decrease the left margin // and increase the right margin. e.PageSettings.Margins.Left -= GUTTER; e.PageSettings.Margins.Right += GUTTER; } }

If this is the first page (the page number is 0), the code adds 100 to the left margin to indent the left side of the page by 1 inch. (Units are hundredths of an inch.)

If this is not the first page but the page number is even, the code adds 100 to the left margin and subtracts 100 from the right margin. This moves the page right while keeping it the same width.

If the page number is odd, the code subtracts 100 from the left margin and adds 100 to the right margin. This moves the page left while keeping it the same width.

The rest of the code (which generates the printout, displays the print preview, and sends a printout to the printer) is similar to the previous example.

The PrintDocument object fires one other event: EndPrint. This event fires when the printout is complete. You should use it to perform any necessary clean up. For example, you can use EndPrint to disconnect from a database, close a file that you opened to make the printout, or otherwise undo whatever you did to produce the printout.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.