я с этим мучался тоже.. вот в принципе это работает....
событие принтПэйдж
| Код | private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { try { 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(PrintString.ToString(), txtPad.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage); // Draws the string within the bounds of the page e.Graphics.DrawString(PrintString.ToString(), txtPad.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed. //printstring = printstring.Substring(charactersOnPage); PrintString.Remove(0, charactersOnPage); // Check to see if more pages are to be printed. e.HasMorePages = (PrintString.Length > 0); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
|
событие нажатия на "пртинт"
| Код | private void mnuFilePrint_Click(object sender, EventArgs e) { this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage); dlgPrint.Document = printDocument1; if (path != "") { printDocument1.DocumentName = Path.GetFileName(path); } else { printDocument1.DocumentName = "Document1"; } dlgPrint.AllowSelection = true; PrintString.Append(txtPad.Text); if (dlgPrint.ShowDialog() == DialogResult.OK) printDocument1.Print(); else return; }
|
переменные инициалируются в самом начале в классе form
| Код | static string path = ""; StringBuilder PrintString = new StringBuilder();
|
код из моей проги, так что надо немного изменить, думаю разберёшься ) |