/ / -------------------------------------------------------------------------------------------- ------------------------------ //
Public class printform: system.windows.Forms.form {///
Private Pagesettings StoredPageSettings = NULL;
Public PrintForm () {// // Windows Form Designer Support for // InitializationComponent ();
// The event handler of the start button PagesetupButton.Click = new system.EventHandler (PagesetupButton_Click); PrintButton.click = new system.eventhandler (printbutton_click);}
// Event void PagesetupButton_Click (Object Sender, Eventargs E) when the user presses the page setting button
Try {PageSetupDialog Psdlg = New PagesetupDialog ();
IF (StoredPageSettings == Null) {storedpagesettings = new Pagesettings ();
Psdlg.pagesettings = storedpagesettings; psdlg.showdialog ();} catch (exception ex) {messagebox.show ("error -" ex. measureage);}
}
// 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); // assume the default printer
IF (storedpagesettings! = null) {pd.defaultpagesettings = storedpagesettings;
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);}}
///
///
}
//
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;}}
}
Link to online sample documentation
Link to online example source code