C # print preview and print [inaccurate printing position]

Source: Internet
Author: User

// Http://dotnet.chinaitlab.com/List_233.html
Private void button4_click (Object sender, eventargs E)
{

// For the preview function, you need to add a control: theprintdocument
String strtext = richtextbox1.text;
Stringreader myreader = new stringreader (strtext );
Printpreviewdialog printpreviewdialog1 = new printpreviewdialog ();
Printpreviewdialog1.document = theprintdocument;
Printpreviewdialog1.formborderstyle = formborderstyle. fixed3d;
Printpreviewdialog1.showdialog ();
}

Private void button5_click (Object sender, eventargs E)
{
// Print function. You need to add a control: printdialog.
Printdialog1.document = theprintdocument;
String strtext = richtextbox1.text;
Stringreader myreader = new stringreader (strtext );
If (printdialog1.showdialog () = dialogresult. OK)
{
// This method calls the theprintdocument_printpage event to complete the print task.
Theprintdocument. Print ();
}
}

Private void theprintdocument_printpage (Object sender, printpageeventargs eV)
{
// Implement the True Print function
String strtext = richtextbox1.text;
Stringreader myreader = new stringreader (strtext );

Float linesperpage = 0;
Float yposition = 0;
Int COUNT = 0;

Float leftmargin = eV. marginbounds. Left;
Float topmargin = eV. marginbounds. Top;

String line = NULL;
Font printfont = richtextbox1.font;
Solidbrush mybrush = new solidbrush (color. Black );

// Calculate the number of rows printed per page
Linesperpage = eV. marginbounds. Height/printfont. getheight (EV. Graphics );

// Repeat the stringreader object to print all the content in richtextbox1
While (count <linesperpage & (line = myreader. Readline ())! = NULL ))
{
// Calculate the location of the page on which the next row is to be printed
Yposition = topmargin + (count * printfont. getheight (EV. Graphics ));
// Print the content of the next line in richtextbox1
Ev. Graphics. drawstring (line, printfont, mybrush, leftmargin, yposition, new stringformat ());
Count ++;
}
// Determine whether to print the next page.
If (line! = NULL)
Ev. hasmorepages = true;
Else
Ev. hasmorepages = false;
Mybrush. Dispose ();
}

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.