Windows Printing - University of South Florida

Download Report

Transcript Windows Printing - University of South Florida

Windows Printing
1
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.
2
Printing
Not in our text.
Covered in
Professional C# 2008
Christian Nagel, et al.
Wrox 2008
ISBN 978-0-470-19137-8
3
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!
4
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
Add a MenuStrip
7
Add a Print Command
8
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.
9
Add a PrintDocument
10
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.
11
Print Command
Double click on the Print command to add an event handler.
12
Print Command Event Handler
13
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.
14
Adding a PrintPage Event Handler
15
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
16
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.
}
17
PrintPage Event Handler

Using default name for the PrintDocument, you get:
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
}
18
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
19
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
20
How to get a PrintPage event

In order to get a PrintPage event
call PrintDocument.Print()
21
The Command Click Handler
private void printToolStripMenuItem_Click(object sender,
EventArgs e)
{
printDocument1.Print();
}
22
Which Printer


Unless we say otherwise, this will print
on the default printer.
Normally can be set via the Control Panel.
23
Setting the Default Printer
24
The Printed Page
25
Printing Graphics

Anything that you can draw on the
screen with GDI+ can also be printed.


The same code is used to draw on the
screen and to print.


Using exactly the same statements.
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
26
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
28
Printing an Image


Just like drawing on the screen.
Add an image to the project as an
embedded resource.
29
Printing an Image
Project Name
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);
30
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.
31
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;
32
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.
33
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;
}
34
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.
35
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.
36
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
37
Margins

e.MarginBounds



Rectangle
Specifies coordinates of normal printing area
e.PageBounds


Rectangle
Specifies coordinates of physical printing area
38
Configuring Printing
The Windows Standard Print Dialog
39
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()
40
Using a PrintDialog
41
Setting PrintDialog Document Property
Right click and select Properties.
Select Document.
42
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();
}
}
43
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;
44
PrintDialog Settings
Upon return from ShowDialog:

Use FromPage and ToPage to control
range of pages printed
45
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();
}
}
46
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;
}
47
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
The PrintPreview Dialog


Will create the dialog programatically.
Setting the dialog’s Document property
to printDocument1 works like calling
printDocument1’s Print() method
49
Add a Print Preview Command
Double click on Print Preview command to add event handler.
50
Creating a PrintPreviewDialog
private void printPreviewToolStripMenuItem_Click(object sender,
EventArgs e)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = printDocument1;
ppd.ShowDialog();
}
51
PrintPreview Dialog
52
PrintPreview Dialog

The PrintPreview dialog handles multiple
pages automatically.


Also handles




You must provide the code for multiple pages in your handler
for the PagePrint event.
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.
53
PrintPreview Dialog
private 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();
}
}
54
PrintPreview Dialog
End of Presentation
55