Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.
Hannah Smalltree, Editorial DirectorThe print document is modeled using the PrintDocument component from the System.Drawing.Printing namespace and is available from the Toolbox. The basic usage of a PrintDocument object is to create an instance, subscribe to at least the PrintPage event, call the Print method and handle the PrintPage event:
PrintDocument printDocument1;
void InitializeComponent() {
this.printDocument1 = new PrintDocument();
...
this.printDocument1.PrintPage +=
new PrintPageEventHandler(this.printDocument1_PrintPage);
...
}
void printButton_Click(object sender, System.EventArgs e) {
printDocument1.DocumentName = fileName;
printDocument1.Print();
}
void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
// Draw onto the e.Graphics object
Graphics g = e.Graphics;
using( Font font = new Font("Lucida Console", 72) ) {
g.DrawString("Hello,nPrinter", font, ...);
}
}
This was first published in January 2003