windows printing

55
1 Windows Printing

Upload: kitra-miranda

Post on 30-Dec-2015

33 views

Category:

Documents


1 download

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

TRANSCRIPT

Page 1: Windows Printing

1

Windows Printing

Page 2: 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.

Page 3: Windows Printing

3

Printing

Not in our text.

Covered in

Professional C# 2008Christian Nagel, et al.Wrox 2008ISBN 978-0-470-19137-8

Page 4: Windows Printing

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!

Page 5: Windows Printing

Getting Started

We will look at simple examples. Concepts scale up to realistic cases.

Create a new Windows Forms project: Print_Demo

5

Page 6: Windows Printing

Print Command

Windows programs typically include a Print command in the File menu. Disabled until there is something to

print.

6

Page 7: Windows Printing

7

Add a MenuStrip

Page 8: Windows Printing

8

Add a Print Command

Page 9: Windows Printing

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.

Page 10: Windows Printing

10

Add a PrintDocument

Page 11: Windows Printing

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.

Page 12: Windows Printing

12

Print Command

Double click on the Print command to add an event handler.

Page 13: Windows Printing

13

Print Command Event Handler

Page 14: Windows Printing

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.

Page 15: Windows Printing

15

Adding a PrintPage Event Handler

Page 16: Windows Printing

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

Page 17: Windows Printing

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.

Page 18: Windows Printing

18

PrintPage Event Handler

Using default name for the PrintDocument, you get:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{ }

Page 19: Windows Printing

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

Page 20: Windows Printing

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

Page 21: Windows Printing

21

How to get a PrintPage event

In order to get a PrintPage eventcall PrintDocument.Print()

Page 22: Windows Printing

22

The Command Click Handler

private void printToolStripMenuItem_Click(object sender,

EventArgs e)

{

printDocument1.Print();

}

Page 23: Windows Printing

23

Which Printer Unless we say otherwise, this will

print on the default printer.

Normally can be set via the Control Panel.

Page 24: Windows Printing

24

Setting the Default Printer

Page 25: Windows Printing

25

The Printed Page

Page 26: Windows Printing

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

Page 27: Windows Printing

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);}

Page 28: Windows Printing

28

Here is the result

Upper left hand corner of page

Page 29: Windows Printing

29

Printing an Image Just like drawing on the screen.

Add an image to the project as an embedded resource.

Page 30: Windows Printing

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

Page 31: Windows Printing

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.

Page 32: Windows Printing

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;

Page 33: Windows Printing

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.

Page 34: Windows Printing

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;}

Page 35: Windows Printing

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.

Page 36: Windows Printing

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.

Page 37: Windows Printing

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

Page 38: Windows Printing

38

Margins

e.MarginBounds Rectangle Specifies coordinates of normal printing

area

e.PageBounds Rectangle Specifies coordinates of physical printing

area

Page 39: Windows Printing

39

Configuring Printing

The Windows Standard Print Dialog

Page 40: Windows Printing

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()

Page 41: Windows Printing

41

Using a PrintDialog

Page 42: Windows Printing

42

Setting PrintDialog Document Property

Right click and select Properties.

Select Document.

Page 43: Windows Printing

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();

}

}

Page 44: Windows Printing

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;

Page 45: Windows Printing

45

PrintDialog Settings

Upon return from ShowDialog:

Use FromPage and ToPage to control range of pages printed

Page 46: Windows Printing

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();

}

}

Page 47: Windows Printing

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;}

Page 48: Windows Printing

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

Page 49: Windows Printing

49

The PrintPreview Dialog

Will create the dialog programatically.

Setting the dialog’s Document property to printDocument1 works like calling printDocument1’s Print() method

Page 50: Windows Printing

50

Add a Print Preview Command

Double click on Print Preview command to add event handler.

Page 51: Windows Printing

51

Creating a PrintPreviewDialog

private void printPreviewToolStripMenuItem_Click(object sender,

EventArgs e)

{

PrintPreviewDialog ppd = new PrintPreviewDialog();

ppd.Document = printDocument1;

ppd.ShowDialog();

}

Page 52: Windows Printing

52

PrintPreview Dialog

Page 53: Windows Printing

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.

Page 54: Windows Printing

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();

}

}

Page 55: Windows Printing

55

PrintPreview Dialog

End of Presentation