由于要点,我还不能发表评论,但是您显示的是添加到事件处理程序的方法,这是我为将文本打印到文档中所做的示例:
向事件处理程序添加方法:
printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage);
事件处理程序方法(您发布的案例):
void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, printFont,
e.MarginBounds.Size, System.Drawing.StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, printFont, System.Drawing.Brushes.Black,
e.MarginBounds, System.Drawing.StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}
关闭打印:
printDocument.Print();