Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Master Everyday Printing in .NET
Create professional-looking reports using the classes built into the .NET Framework.
by Michael Eaton

November 2002 Issue

Technology Toolbox: C#

ADVERTISEMENT

You can program .NET to produce printed output, such as reports, in several ways. Crystal Reports, supplied with Visual Studio, is the worldwide de facto reporting tool for Windows programmers, but it's overkill for less complex reports. Of course, you could use good old Win32 API calls, but while the API gives you total control, it also locks you into a single platform. If Microsoft or another company (such as Ximian, with its Mono project) moves the Framework to other platforms, programs using the API will still be stuck on Windows unless you rewrite them for the new platform.

The .NET Framework gives you a new alternative to these printing methods: the System.Drawing.Printing namespace, whose classes combine the fine-grain control of the Win32 API with the relative ease of Visual Basic's classic Printer object (see Table 1). Here I'll focus on this namespace's primary classes, which let you produce moderately complex reports. The PrintDocument class is the most important of these classes. It lets you define an object that sends output to a destination, which could be a printer or a print preview display (using the PrintPreviewDialog class). I'll also discuss the PrinterSettings class, which allows you to control how the document is printed.

You can easily create a PrintDocument object in C# and hook it up to an event handler (the code that actually handles the printing):

PrintDocument doc = new 
   PrintDocument();
   doc.PrintPage += new 
      PrintPageEventHandler(
      doc_PrintPage);

The PrintPage event handler takes an argument of type PrintPageEventArgs. The argument contains data related to this event, including page settings for the current page, margin information, and whether there are more pages to be printed. The event fires once per page until ev.HasMorePages equals false:

private void doc_PrintPage(object 
   sender, PrintPageEventArgs ev)



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home