Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Printing a Document. 2 Objectives You will be able to print a real multipage document.

Similar presentations


Presentation on theme: "1 Printing a Document. 2 Objectives You will be able to print a real multipage document."— Presentation transcript:

1 1 Printing a Document

2 2 Objectives You will be able to print a real multipage document.

3 3 Getting Started Start with Schedule Viewer program from last class: http://www.cse.usf.edu/~turnerr/Software_Systems_Development/ Downloads/2011_03_22_Scroll_Bars/ http://www.cse.usf.edu/~turnerr/Software_Systems_Development/ Downloads/2011_03_22_Scroll_Bars/ File Schedule_Viewer_with_Scrolling.zip Enable the Print command when a file is opened. Add to openToolStripMenuItem_Click(): printToolStripMenuItem.Enabled = true;

4 4 State Information for Printing public partial class Form1 : Form {... // Print information int Lines_per_Page; int Next_Page; int Last_Page;

5 5 The Menu Double click on Print to add a command event handler.

6 6 Print Command Handler

7 7 Print Dialog Drag a PrintDocument to the form. Drag a PrintDialog to the form. Set the PrintDialog’s Document property to PrintDocument1.

8 8 Print Dialog

9 Print Command We need an event handler for a click on the Print command. In Design mode, double click on the menu command to add an event handler. Copy: http://www.cse.usf.edu/~turnerr/Software_Systems_Development/ Downloads/2011_03_22_Scroll_Bars/printToolStripMenuItem_Click.cs http://www.cse.usf.edu/~turnerr/Software_Systems_Development/ Downloads/2011_03_22_Scroll_Bars/printToolStripMenuItem_Click.cs 9

10 10 Print Command Handler private void printToolStripMenuItem_Click( object sender, EventArgs e) { Lines_per_Page = 15; // Force multiple pages int Number_of_Pages = Schedule.Count / Lines_per_Page; if (Schedule.Count % Lines_per_Page > 0) { Number_of_Pages++; } 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;

11 11 Print Command Handler if (printDialog1.ShowDialog() == DialogResult.OK) { Next_Page = printDialog1.PrinterSettings.FromPage; Last_Page = printDialog1.PrinterSettings.ToPage; printDocument1.Print(); }

12 12 Add PrintPage Event Handler Double click on printDocument1. Visual Studio adds a skeleton PrintPage event handler. private void printDocument1_PrintPage( object sender, System.Drawing.Printing.PrintPageEventArgs e) { } Fill in the event handler. http://www.cse.usf.edu/~turnerr/Software_Systems_Development/ Downloads/2011_03_22_Scroll_Bars/printDocument1_PrintPage.cs http://www.cse.usf.edu/~turnerr/Software_Systems_Development/ Downloads/2011_03_22_Scroll_Bars/printDocument1_PrintPage.cs

13 PrintPage Event Handler private void printDocument1_PrintPage( object sender, System.Drawing.Printing.PrintPageEventArgs e) { int Print_Y_Pos = e.MarginBounds.Top; int First_Output_Line = (Next_Page - 1) * Lines_per_Page; int Last_Output_Line = First_Output_Line + Lines_per_Page - 1; if (Last_Output_Line >= Schedule.Count) { Last_Output_Line = Schedule.Count - 1; } for (int i = First_Output_Line; i <= Last_Output_Line; i++) { Point P = new Point(e.MarginBounds.Left, Print_Y_Pos); Schedule_Record se = Schedule[i]; se.Output(e.Graphics, Font1, Brush1, P); Print_Y_Pos += Font1.Height; } Next_Page++; e.HasMorePages = (Next_Page <= Last_Page); }

14 14 Printing a Schedule Entry The actual output to the printer is done by the same Schedule_Record method that draws a schedule entry on the screen. Schedule_Record.Output The Graphics object for output to the printer comes from the PrintPageEventArgs parameter of the PrintPage event handler.

15 15 Build and Test Should print three pages.

16 16 The PrintPreview Dialog Let’s add a Print Preview Dialog. Permit the user to view what would be printed on the screen.

17 17 PrintPreview Dialog The PrintPreview dialog handles multiple pages automatically You must provide the code to print multiple pages in your handler for the PagePrint event. Also handles Zoom View multiple pages Print No additional effort on your part! But be sure any initialization done for the print command is also done for print preview.

18 18 Add Print Preview Command Double click on Print Preview to create event handler.

19 19 Add Print Preview Command Enable Print Preview command when file is opened. private void openToolStripMenuItem_Click(object sender, EventArgs e) { import_schedule();... openToolStripMenuItem.Enabled = false; printToolStripMenuItem.Enabled = true; printPreviewToolStripMenuItem.Enabled = true; }

20 20 The PrintPreview Command Handler private void printPreviewToolStripMenuItem_Click( object sender, EventArgs e) { Initialize_Printing(); if (printDialog1.ShowDialog() == DialogResult.OK) { Next_Page = printDialog1.PrinterSettings.FromPage; Last_Page = printDialog1.PrinterSettings.ToPage; PrintPreviewDialog ppd = new PrintPreviewDialog(); ppd.Document = printDocument1; ppd.ShowDialog(); }

21 21 The PrintPreview Command Handler Add function Initialize_Printing() Move initialization code from Print command handler. Same initialization for Print and PrintPreview.

22 22 Initialize_Printing() private void Initialize_Printing() { Lines_per_Page = 15; // Force multiple pages Number_of_Pages = Schedule.Count / Lines_per_Page; if (Schedule.Count % Lines_per_Page > 0) { Number_of_Pages++; } 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; }

23 23 Revised Print Command Handler private void printToolStripMenuItem_Click( object sender, EventArgs e) { Initialize_Printing(); if (printDialog1.ShowDialog() == DialogResult.OK) { Next_Page = printDialog1.PrinterSettings.FromPage; printDocument1.Print(); }

24 24 PrintPreview Dialog


Download ppt "1 Printing a Document. 2 Objectives You will be able to print a real multipage document."

Similar presentations


Ads by Google