Main formCodeAs follows:
Copy code The Code is as follows: public partial class printfileform: Form
{
Public printfileform ()
{
Initializecomponent ();
Printfile prinfile = new printfile ();
Prinfile. Print ();
}
}
The printed file category is as follows:
Copy code The Code is as follows: Class printfile
{
Streamreader sr = NULL;
Font printfont = new font ("", 12 );
Public void print ()
{
Try
{
Sr = new streamreader (@ "F: \ temp.txt ");
Try
{
Printdocument printdoc = new printdocument ();
Printdoc. printpage + = printdoc_printpage;
Printdoc. Print ();
}
Finally
{
Sr. Close ();
}
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
}
Void printdoc_printpage (Object sender, printpageeventargs E)
{
String line = NULL;
// Set the number of rows on one page = the height of the print area divided by the font height.
Float pageline = E. marginbounds. Height/printfont. getheight (E. Graphics );
// Print each row cyclically
For (int count = 0; count <pageline & (line = Sr. Readline ())! = NULL); count ++)
{
Float singleline = E. marginbounds. Top + (count * printfont. getheight (E. Graphics ));
E. Graphics. drawstring (line, printfont, brushes. Black, E. marginbounds. Left, singleline );
}
// Determine whether to continue printing
If (line! = NULL)
E. hasmorepages = true;
Else
E. hasmorepages = false;
}
}