Download presentation
Presentation is loading. Please wait.
1
Printing Petzold, Chapter 21
Jim Fawcett CSE778 – Advanced Windows Programming Summer 2003 Derived from a presentation by Ryan Nugent and Ed Friers
2
Windows Form Printing A printer is described by an object of type PrinterSettings. Defined in the System.Drawing.Printing namespace. PrinterSettings prnset = new PrinterSettings();
3
Printer Setting Properties
PrinterName usually indicates the make and model of the printer, but the user can change the name during installation. PrinterName is writable, but if it is changed, all the other properties are affected. If PrinterName is not the name of a printer, IsValid is set to false. (no exception thrown)
4
Printer Setting Properties (con’t)
IsValid and IsDefaultPrinter are true when you create a new PrinterSettings object as long as there is an installed printer. If no printers installed, PrinterName is <no default printer> and IsValid is false.
5
Static Property of Printer Settings
This gives you a list of all the installed printers. using PrinterSettings; StringCollection sc = InstalledPrinters();
6
Accessing the List of Printers
You can use the count to get the number of printers: sc.Count You can access them using []: sc[0] or sc[1] …… sc[count-1] You can access them directly: PrinterSettings.InstalledPrinters[1]
7
Using the List of Printers
Code that puts all the installed printers in a combo box; foreach(string str in PrinterSettings.InstalledPrinters) combo.Items.Add(str); You can assign the PrinterName directly prnset.PrinterName=sc[2]; IsValid is true, IsDefaultPrinter is false even if you set the PrinterName to the default printer.
8
Printer Settings Properties
If IsPlotter = True then the printer should not be relied on to print bitmaps. LandscapeAngle is either 90 or 270 degrees. If it is not an available feature, it is set to 0.
9
Printer Setting Properties
These can be accessed with either Count or an indexer. Each property will be discussed in the next few slides.
10
Paper Source Properties
This is a PrinterSettings Property. It is a collection of all the paper sources on the printer, not the default. An example of a SourceName is: “Manual Paper Feed”
11
Paper Source Kind Enumeration
12
Paper Size Properties Width and Height indicate the size of the paper in hundredths of an inch. PaperSize is a property of PrinterSettings.
13
Paper Kind Enumeration
Some of the more useful members (117 members total).
14
Printer Resolution Properties
This is a PrinterSettings property. X and Y indicate the actual dots per inch.
15
PrinterResolutionKind Enumeration
Every printer has at least one of each of these. Each of the resolutions the printer can provide gives you a Custom value. The others have a X and Y value of –1.
16
Printer Setting Properties
If CanDuplex is True, you can print on both sides of the page. Only can use enum values if True. Simplex is one-sided printing. Vertical and Horizontal show how the pages are connected.
17
Printer Setting Properties
Copies is 1 by default, can be set up to max copies. If collate is True, the pages are printed 1,2,3,1,2,3. Default depends on the printer.
18
Printer Settings for Print Dialog
These are normally used with the PrintDialog class that will be discussed later.
19
Page Settings This class describes the characteristics of a printed page. This property indicates the default page settings. Will be discussed more later.
20
Page Settings PageSettings class describes those printer characteristics that can change with each page. Each object is associated with a particular printer. You can create a PageSettings object or use a pre-created one (from PrinterSettings). PageSettings() PageSettings(PrinterSettings prnset)
21
Intro to Page Settings Properties
Indicates printer these settings are associated with. More on the next slide.
22
Page Settings Properties
A Windows Form program can change default settings when printing a document, but these changes DO NOT affect other applications. prnset == prnset.DefaultPageSettings.PrinterSettings is True because the object is only a reference, changes affect both. Pageset==pageset.PrinterSettings.DefaultPageSettings is False, only initially true.
23
Page Settings Properties
Landscape toggles between Landscape and Portrait. Color tells if color will be used on the page. Bounds defines a rectangle object that indicates the size of the page units in hundredths of an inch. According to paper size and landscape settings.
24
Page Settings Properties - Margins
This indicates the default margins for a page, initially set to 1 inch on all sides. Constructors: Margins() or Margins (int Right, Left, Top, Bottom) Properties: Right, Left, Top, Bottom
25
Page Settings Properties
Similar to PrinterSettings class properties The PaperSource property of PageSettings is one of the items from PaperSources collection in PrinterSettings.
26
Notes on the Previous Slide
If you change one of these three properties in your program, be sure to set the property from a member of the corresponding collection. PaperSize property is not affected by the Landscape Property.
27
Defining a Document A print job is represented by the PrintDocument Class. It only has a default constructor. A program begins the printing process by creating an object of type PrintDocument. If this is a field, you only need to create it once, and it retains all settings.
28
Print Document Properties
When you create a new PrintDocument object, it uses the default printer. The DefaultPageSettings property is set from the DefaultPageSetting property of PrinterSettings.
29
Notes on the Properties
DocumentName is set to text string “document”…it should be changed. See PrintController, Slide 40. prndoc.PrinterSettings ==prndoc.DefaultPageSettings.PrinterSettings Is True. prndoc.DefaultPageSettings==prndoc.PrinterSettings.DefaultPageSettings Is False so you can change default settings for the document, not for the printer.
30
Print Document Events Begin and end are triggered once for each print job. QueryPageSetting and PrintPage are triggered for every page.
31
Notes on the Events PrintPage event handlers indicate if there are more pages to be printed. If you want to have different settings for each page, install a handler for QueryPageSettings. For initialization or cleanup, install handlers for BeginPrint or EndPrint.
32
Print You initiate printing by calling this method:
Void Print() The print method doesn’t return until the program is finished printing the document. The application can’t respond to any input during this time.
33
Setting Print Page Event Args
BeginPrint event handler can set Cancel to true to abort the print job. QueryPageSetting can change PageSettings properties in preparation for the corresponding PrintPage event.
34
Print Page Event Args The graphics object is created new for each page. If you set PageUnit or PageScale, it will not stay.
35
Notes on Print Page Event Args
HasMorePages is always false; must be set to true to continue. Cancel is usually set to false. PageSetting will reflect any changes made in QueryPageSettings.
36
Printer Selection Dialog Program
The simple dialog box shown above is used in the PrintThreePage and PrintWithMargin programs.
37
Print Three Pages Program
Most of the work is going on in the MenuFilePrintOnClick method. PrintDocument and PrintSelectionDialog objects are created. Selected printer, resolution and other properties are set. OnQueryPageSetting alternates between Landscape and Portrait. Code on
38
Page Dimensions In the Windows Form interface, you can never tell exactly what the margins are. This is because you can’t tell what the printer can physically print on. The margins aren’t the same between left and right or top and bottom. You can approximate the margins as in the PrintwithMargins program on pages 1020 and 1021. The OnPrintPage method makes an approximate rectangle.
39
PrinterUnitConvert Static Convert Method
This method has 6 versions, each with different first argument types: int, double, Point, Size, Rectangle, Margins; the general form is: Type Convert(type tValue, PrinterUnit puFrom, PrinterUnit puTo)
40
The Print Controller PrintController class is abstract.
Derived classes are: StandardPrintController. PrintControllerWithStatusDialog (default). PreviewPrintController.
41
PrintController Class Methods
Four methods of PrintController are: OnStartPrint, OnStartPage, OnEndPage and OnEndPrint. These methods are called by the PrintDocument between BeginPrint.. EndPrint. PreviewPrintController uses OnStartPage to get Graphics object to display in print preview. Otherwise, the return object goes to PrintPage event handler.
42
An Example The program on pp shows how to display current printing document and page with a status bar instead of a dialog box. This task is easily accomplished using StandardPrintController and overriding the four PrintController methods.
43
Using the Standard Print Dialog Box
PrintDialog makes a dialog box that lets user select and change settings for a printer and specify what portion of the document to print. When creating a new PrintDialog object using default constructor, you must initialize one (but not both) of: Document is preferred as it also sets PrinterSettings to the same property of the PrintDocument.
44
More PrintDialog Properties
These properties are options the dialog gives the user.
45
PrintDialog’s PrinterSettings Properties
46
Simple Print Dialog This program only allows you to bring up a print dialog box and print the numbers 1-3 thru the dialog box. The only option that is allowed is the AllowSomePages. If you move the first three lines of MenuFilePrintOnClick, you keep the settings every time. Code on
47
Setting Up the Page PageSetupDialog is a dialog box for specifying margins, page orientation, page sources and paper sizes as well as selecting default printer and options. When creating a new PageSetupDialog with default constructor, set one (only one) of: Document property is preferred as this will set others.
48
More PageSetupDialog Properties
All bools are default true except ShowHelp. MinMargins default is all zeros.
49
ImagePrint Program This program lets the user load, save, and print bitmaps using a menu and the 2 dialog boxes. ImagePrint class is derived from class ImageIO from the program in Chapter 16. OnPrintPage method calculates a display rectangle and makes the image as large as possible within the rectangle.
50
Print Preview To perform a preview, the PrintPage event handler is used to display output to the surfaces of bitmaps. When the print method returns, the Bitmaps are then presented to the user.
51
The Easy Way Create an object: Just do it:
PrintPreviewDialog predlg = new PrintPreviewDialog; Just do it: Predlg.Document = prndoc; Predlg.ShowDialog(); ShowDialog does all the work for you. You can even print from this if you set DocumentName property of PrintDocument.
52
The Program The PrintDialog dialog box lets you print a selection or range of pages. Set as fields so you save settings. You should be able to print from the print preview screen. Word Wrap v. Trimming. Code on pages
53
End of Presentation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.