Page Layout and Printing

Team Fly 

Page 168

Page Layout and Printing

The process of printing is identical to displaying graphics on a Form or PictureBox control. You can use any of the drawing methods of the Graphics object that represents the page. It's your responsibility to place the graphics elements on the page and determine when the current page has been filled and start printing a new page.

The origin of the page is its upper-left corner; the coordinates of this point are (0, 0). The origin of the printed element is the upper-left corner, too. If you print a string at coordinates (0, 0) it will be printed just inside the page, but it will be printed in its entirety. If you print the same string at the lower-left or lower-right corner of the page, nothing will appear on the printout.

The default unit of the page is a hundredth of an inch (there are 100 units in an inch). A lettersized page's dimensions are 850×1100. Every professional-looking printout has a respectable margin on all four edges. Printouts that cover the entire page look very odd—you've probably never generated such a printout. You can change the default units by setting the PageUnit property of the Graphics object to one of the members of the System.Drawing.GraphicsUnit enumeration. Among the members of this enumeration are Inch, Millimeter, Point, Pixel, Display (1/75th of an inch), and Document (1/300th of an inch). You can also use your own units by setting the PageUnit property to World.

However, not all printers can cover the entire page. There's a small margin that laser printers ignore, a very small margin compared to the user-specified margin. The printer's margin is usually a tenth of an inch. In most cases, we don't care about this margin, because the user-specified margin is much larger. However, if you plan to create printouts that cover the entire page, you must take into consideration the printer margin. The printer margin will cause a very disconcerting problem, namely a discrepancy between the preview and the actual printout. The monitor has no such margin, so you can preview graphics elements very near the edges of the page. When the same printout is sent to the printer, the printer margin may affect the appearance of the printout—it will not be identical to the page's preview on the monitor. Let's consider the printout of a rectangle that fills the entire page. If you preview the printout you'll see a rectangle that fills the page as expected. If you send the same document to a laser printer, the rectangle's origin will be displaced by a tenth of an inch (or so) from the upper-left corner of the page. The lower-right corner of the page will end up outside the page. If you print a rectangle that fills the printable area of the page (that is, the entire page excluding the user-specified margins), the rectangle will be the same, both on the preview pane and the printed page.

This behavior is caused by the fact that any shape whose origin falls within the printer margin is displaced slightly. If the element's origin is within the printable area of the page, the element is not displaced. To handle the printer margin, you can use the RenderingOrigin property of the Graphics object that represents the page. The RenderingOrigin property exposes the X and Y properties, which are the coordinates of the top-left point that can be printed on the page.

NOTE The RenderingOrigin property of the Graphics object is new to version 1.1 of the .NET Framework. If you're using version 1.0 of the Framework, you can't use this property.

The DrawString and MeasureString Methods

You can use any of the Graphics object's methods to draw shapes, but for business applications the method you'll be using the most is the DrawString method, which renders strings in a specified font with a specified brush on the printer's page. Printing text is not a trivial operation, as you will see

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net