550 likes | 713 Views
Windows Printing. Objectives. You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print dialog. Provide a Print Preview function in your programs. Printing. Not in our text. Covered in Professional C# 2008
E N D
Objectives You will be able to • Output text and graphics to a printer. • Print multipage documents. • Use the standard Windows print dialog. • Provide a Print Preview function in your programs.
Printing Not in our text. Covered in Professional C# 2008 Christian Nagel, et al. Wrox 2008 ISBN 978-0-470-19137-8
Printing • Printing in Windows is similar to drawing on the screen with GDI+. • You draw on a page just like you draw on a Windows form. • But there are some differences: • The printed page has a fixed size. • Scroll bars don’t work!
Getting Started • We will look at simple examples. • Concepts scale up to realistic cases. • Create a new Windows Forms project: • Print_Demo
Print Command • Windows programs typically include a Print command in the File menu. • Disabled until there is something to print.
Printing To print in a Windows application you need a PrintDocument object. • In designer mode Drag from the toolbox and drop on the form • PrintDocument1 appears in the component tray. • You can also create a PrintDocument object programatically.
Printing • Print operations are done in an event handler for the PrintPage event. • To initiate printing, call the Print() method of your PrintDocument control • Example: printDocument1.Print(); • Typically this is done in the click event handler for a Print command in the File menu. • Results in a PrintPage event.
Print Command Double click on the Print command to add an event handler.
Printing • The actual output is done in an event handler for the PrintPage event of the PrintDocument. • In design mode • Right click on the PrintDocument control (in the component tray) to open its Properties window • Click on the Events icon (lightening bolt) to display applicable events • Beside “PrintPage” type the name that you want to give to your PrintPage event handler. • Or just double click. • Visual Studio creates a stub event handler and opens the edit window at that point.
PrintPage Event Handler • Or, the easy way – • Just double click on the PrintDocument control in the component tray. • An event handler stub will be generated with a default name based on the name of the PrintDocument
PrintPage Event Handler private void printToolStripMenuItem_Click(object sender, EventArgs e) { } Automatically generated stub for the PrintDocument PrintPage handler private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { } Enter your print statements here.
PrintPage Event Handler • Using default name for the PrintDocument, you get: private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { }
How to output to the printer • Outputing to a printed page is exactly the same as drawing on a form. • You get a Graphics object in an argument to the PrintPage event handler. • Use this Graphics object for any drawing operation. • Upon exit from the event handler, whatever was drawn is sent to the printer as a single page. • Default printer unless you say otherwise
Printing Example This argument includes the Graphics object that you use to do output to the printer. private void PrintPage (object sender, System.Drawing.Printing.PrintPageEventArgs e) { Font myFont = new Font("Arial", 24); e.Graphics.DrawString ("Hello, World!", myFont, Brushes.Black, 30, 30); } (X,Y) coordinates for where on page to start the string
How to get a PrintPage event • In order to get a PrintPage event call PrintDocument.Print()
The Command Click Handler private void printToolStripMenuItem_Click(object sender, EventArgs e) { printDocument1.Print(); }
Which Printer • Unless we say otherwise, this will print on the default printer. • Normally can be set via the Control Panel.
Printing Graphics • Anything that you can draw on the screen with GDI+ can also be printed. • Using exactly the same statements. • The same code is used to draw on the screen and to print. • Different Graphics objects determine which is done. • http://www.cse.usf.edu/~turnerr/Software_Systems_Development/Downloads/2011_03_08_Windows_Printing/ PrintPage.cpp.txt
private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Font myFont = new Font("Arial", 12); Rectangle boundingRect = new Rectangle (220, 20, 70, 200); e.Graphics.DrawString ( "The quick brown fox jumped over the lazy dog's back.", myFont, SystemBrushes.WindowText, boundingRect); e.Graphics.DrawRectangle (SystemPens.WindowText, boundingRect); Point topLeft = new Point (20, 20); Point bottomRight = new Point (200, 200); e.Graphics.DrawLine (SystemPens.WindowText, topLeft, bottomRight); Pen redPen = new Pen(Color.Red, 4); // Create rectangle for ellipse. Rectangle rect = new Rectangle( 50, 50, 200, 100); // Draw ellipse to screen. e.Graphics.DrawEllipse(redPen, rect); }
Here is the result Upper left hand corner of page
Printing an Image • Just like drawing on the screen. • Add an image to the project as an embedded resource.
Printing an Image String resource_name = @"Print_Demo.USF_Bull_small.jpg"; System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream s = a.GetManifestResourceStream(resource_name); Image bull = Image.FromStream(s); Point p = new Point(100, 100); e.Graphics.DrawImage(bull, p); Project Name
Multiple Pages What if you want to print more than one page? • It’s a little more complicated. • The program must print one page at a time on successive PrintPage events. • Paginate the document. • Signal the system that you have more pages to print. • Keep track of where you are in your output.
Multipage Output • To tell the system you have more pages to print, in the PrintPage event handler say: e.HasMorePages = true; • When printing the final page, say e.HasMorePages = false;
Multipage Output • There will be a separate call to the PrintPage event handler for each page. • Keeps happening as long as you set e.HasMorePages to true • You have to keep track of where you are in your output. • Cannot use a local variable in PrintPage event handler. • Class member variable in the form class.
Multipage Output private int Next_Page; private int Number_of_Pages; private void printToolStripMenuItem_Click(object sender, System.EventArgs e) { Next_Page = 1; Number_of_Pages = 3; printDocument1.Print(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { String Output_Line = "This is page " + Next_Page.ToString(); Font myFont = new Font("Arial", 24); e.Graphics.DrawString(Output_Line, myFont, Brushes.Black, 30, 30); Next_Page++; e.HasMorePages = Next_Page <= Number_of_Pages; }
Keep in mind • You have to say exactly where to put each line of text on the page. • There is no “new line” concept. • When outputting multiple lines per page • Keep track of vertical position. • Know where you are relative to the margins. • Output page and start a new one as necessary.
Going to a New Line • Class Font has a “Height” property • How far to move down for a new line. • Suppose Point p is the current output position, and you are using Font f. • For new line: • Add f.Height to p.Y. • Reset p.X to left margin.
Multipage Output How do you tell the printer to output another page in a multipage document? • Return from the PrintPage event handler. • The PrintPage event handler will be called again and again for successive pages until you set e.HasMorePages to false
Margins • e.MarginBounds • Rectangle • Specifies coordinates of normal printing area • e.PageBounds • Rectangle • Specifies coordinates of physical printing area
Configuring Printing The Windows Standard Print Dialog
Using a PrintDialog • Drag the PrintDialog component from the tool box to the form. • PrintDialog1 appears in the component tray. • Right click and select properties. • Set its Document property to your PrintDocument • Before calling PrintDocument.Print() call PrintDialog1.ShowDialog()
Right click and select Properties. Select Document. Setting PrintDialog Document Property
Print Dialog • The Cancel button in the PrintDialog does not automatically prevent printing. • You have to check the result returned by ShowDialog() private void printToolStripMenuItem_Click(object sender, EventArgs e) { if (printDialog1.ShowDialog() == DialogResult.OK) { Next_Page = 1; Number_of_Pages = 3; printDocument1.Print(); } }
PrintDialog Settings • Set before calling ShowDialog printDialog1.AllowSelection = false; printDialog1.AllowSomePages = true; printDialog1.PrinterSettings.MinimumPage = 1; printDialog1.PrinterSettings.MaximumPage = Number_of_Pages; printDialog1.PrinterSettings.FromPage = 1; printDialog1.PrinterSettings.ToPage = Number_of_Pages;
PrintDialog Settings Upon return from ShowDialog: • Use FromPage and ToPage to control range of pages printed
Using a PrintDialog private void printToolStripMenuItem_Click(object sender, EventArgs e) { Number_of_Pages = 3; printDialog1.AllowSelection = false; printDialog1.AllowSomePages = true; printDialog1.PrinterSettings.MinimumPage = 1; printDialog1.PrinterSettings.MaximumPage = Number_of_Pages; printDialog1.PrinterSettings.FromPage = 1; printDialog1.PrinterSettings.ToPage = Number_of_Pages; if (printDialog1.ShowDialog() == DialogResult.OK) { Next_Page = printDialog1.PrinterSettings.FromPage; printDocument1.Print(); } }
Using a PrintDialog private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { String Output_Line = "This is page " + Next_Page.ToString(); Font myFont = new Font("Arial", 24); e.Graphics.DrawString(Output_Line, myFont, Brushes.Black, 30, 30); Next_Page++; e.HasMorePages = current_print_page_number <= printDialog1.PrinterSettings.ToPage; }
Print Preview • Lets the user see an image of the printed output on the screen before printing. • Avoid wasting paper! • Very easy to do! • Once we have the basic printing functionality.
The PrintPreview Dialog • Will create the dialog programatically. • Setting the dialog’s Document property to printDocument1 works like calling printDocument1’s Print() method
Add a Print Preview Command Double click on Print Preview command to add event handler.