A contract printing function has been prepared recently. The text in the contract is rich text, which has a certain format but is not limited to the content entered. I have not found any related articles on the Internet. I wrote a notepad printing function below, but I can only use the same font and change pages.
String strprinttext;
Private void printdocumentemediprintpage (Object sender, system. Drawing. Printing. printpageeventargs E)
{
System. Drawing. Font font = new font ("", 14 );
Graphics G = E. graphics;
Float cyfont = font. getheight (g );
Stringformat strfmt = new stringformat ();
Rectanglef rectffull, rectftext;
Int ichars, ilines;
If (G. visibleclipbounds. x <0)
{
Rectffull = E. marginbounds;
}
Else
{
Rectffull = new rectanglef (
E. marginbounds. Left-(E. pagebounds. Width-
G. visibleclipbounds. width)/2,
E. marginbounds. Top-(E. pagebounds. Height-
G. visibleclipbounds. Height)/2,
E. marginbounds. Width, E. marginbounds. Height );
}
Rectftext = rectanglef. Inflate (rectffull, 0,-2 * cyfont); // remove the two rows and print the title and page number.
Int idisplaylines = (INT) math. Floor (rectftext. Height/cyfont); // calculate the number of rows that can be printed
Rectftext. Height = idisplaylines * cyfont; // calculate the height that can be officially printed
Strfmt. trimming = stringtrimming. word;
If (strprinttext. Length = 0)
{
E. Cancel = true;
Return;
}
G. drawstring (strprinttext, Font, brushes. Black, rectftext, strfmt); // print all text
G. measurestring (strprinttext, Font, rectftext. Size, strfmt, out ichars, out ilines); // gets how many characters can be printed in this area
Strprinttext = strprinttext. substring (ichars); // remove the printed characters.
E. hasmorepages = (strprinttext. length> 0); // determines whether there are other words to print.
}
Private void printdocument1_beginprint (Object sender, system. Drawing. Printing. printeventargs E)
{
Strprinttext = richtextbox1.text;
}
I wonder if there is any good way to deal with rich text.