C # simple printing of instance code

Source: Internet
Author: User

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;

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.