/ / -------------------------------------------------------------------------------------------- ------------------------------ //
Public class printform: system.windows.Forms.form {///
Public PrintForm () {// // Windows Form Designer Support for // InitializationComponent ();
// Start the event handler of the print button PrintButButton.click = new system.eventhandler (printbutton_click);
///
// Event void printButton_Click (Object Sender, Eventargs E) {Try {in the user presses the print button
StreamReader streamToPrint = new StreamReader ( "PrintMe.Txt"); try {TextFilePrintDocument pd = new TextFilePrintDocument (streamToPrint); // assumed default printer PrintDialog dlg = new PrintDialog (); dlg.Document = pd; DialogResult result = dlg.ShowDialog ();
IF (result == DialogResult.ok) {pd.print ();}
} Finally {streamtoprint.close ();
} Catch (exception ex) {messagebox.show ("error occurred while printing file -" ex. amount);}}
///
THIS.TEXT = "Print Example 3"; this.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (504, 381); printbutton.imageAlign = system.drawing. ContentAlignment.MiddleLeft; printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; printButton.Size = new System.Drawing.Size (136, 40); printButton.TabIndex = 0; printButton.Location = new System.Drawing.Point (32, 112); PrintButton.text = "Print file"; this.controls.add (this.printbutton);}
///
//
Public TextFilePrintDocument: base () {this.streamtoprint = streamtoprint;}
// Override OnBeginPrint to set the font protected overrid onbeginprint (printeventargs ev) {base.onbeginprint (ev); printfont = new font ("arial", 10);}
// Override OnPrintPage to provide documentation to provide print logic protected override void onprintpage (PrintPageEventArgs EV) {
Base.onPrintPage (EV);
FLOAT LPP = 0; float ypos = 0; int count = 0; float leftmargin = ev.Marginbounds.Left; float TopMargin = ev. marginbounds.top; string line = null;
// Row number of rows per page // Use marginbounds on the event to reach this LPP = ev.MarginBounds.Height / PrintFont.getHeight (ev.graphics);
// Now, repeat this operation on the file to output per row // Note: Assume that a single line is narrow // first check the number of rows in order to see the line of lines that do not printed ((line = streamtoprint. Readline ())! = NULL)) {YPOS = TopMargin (Count * PrintFont.getHeight (ev.graphics));
Ev.graphics.drawstring (Line, PrintFont, Brushes.black, leftmargin, ypos, new stringformat (); count ;}
// If there is a multi line, another page IF (line! = Null) ev.hasmorepages = true; Else ev.hasmorepages = false;}
}
}