Upload
kitra-miranda
View
33
Download
1
Tags:
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
1
Windows Printing
2
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.
3
Printing
Not in our text.
Covered in
Professional C# 2008Christian Nagel, et al.Wrox 2008ISBN 978-0-470-19137-8
4
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
5
Print Command
Windows programs typically include a Print command in the File menu. Disabled until there is something to
print.
6
7
Add a MenuStrip
8
Add a Print Command
9
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.
10
Add a PrintDocument
11
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.
12
Print Command
Double click on the Print command to add an event handler.
13
Print Command Event Handler
14
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.
15
Adding a PrintPage Event Handler
16
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
17
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.
18
PrintPage Event Handler
Using default name for the PrintDocument, you get:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{ }
19
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
20
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);
}
Printing ExampleThis argument includes the Graphics object that you use to do output to the printer.
(X,Y) coordinates for where on page to start the string
21
How to get a PrintPage event
In order to get a PrintPage eventcall PrintDocument.Print()
22
The Command Click Handler
private void printToolStripMenuItem_Click(object sender,
EventArgs e)
{
printDocument1.Print();
}
23
Which Printer Unless we say otherwise, this will
print on the default printer.
Normally can be set via the Control Panel.
24
Setting the Default Printer
25
The Printed Page
26
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);}
28
Here is the result
Upper left hand corner of page
29
Printing an Image Just like drawing on the screen.
Add an image to the project as an embedded resource.
30
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
31
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.
32
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;
33
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.
34
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;}
35
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.
36
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.
37
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
38
Margins
e.MarginBounds Rectangle Specifies coordinates of normal printing
area
e.PageBounds Rectangle Specifies coordinates of physical printing
area
39
Configuring Printing
The Windows Standard Print Dialog
40
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()
41
Using a PrintDialog
42
Setting PrintDialog Document Property
Right click and select Properties.
Select Document.
43
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();
}
}
44
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;
45
PrintDialog Settings
Upon return from ShowDialog:
Use FromPage and ToPage to control range of pages printed
46
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();
}
}
47
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.
48
49
The PrintPreview Dialog
Will create the dialog programatically.
Setting the dialog’s Document property to printDocument1 works like calling printDocument1’s Print() method
50
Add a Print Preview Command
Double click on Print Preview command to add event handler.
51
Creating a PrintPreviewDialog
private void printPreviewToolStripMenuItem_Click(object sender,
EventArgs e)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = printDocument1;
ppd.ShowDialog();
}
52
PrintPreview Dialog
53
PrintPreview Dialog
The PrintPreview dialog handles multiple pages automatically.
You must provide the code for 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.
54
PrintPreview Dialogprivate void btnPreview_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;
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = printDocument1;
ppd.ShowDialog();
}
}
55
PrintPreview Dialog
End of Presentation