Print from RichTextbox blank print page

  • Thread starter Thread starter Mike8009
  • Start date Start date
M

Mike8009

Guest





publicvoidPreviewToolStripMenuItem_Click(objectsender, System.EventArgse)


{




try


{






PrintDialogprintDialog = newPrintDialog();


PrintPreviewDialog1.Document = PrintDocument1;

PrintPreviewDialog1.ShowDialog();






if(printDialog.ShowDialog() == DialogResult.OK)


{






StringReaderreader = newStringReader(rtbDoc.Text);




PrintDocument1.PrintPage += PrintDocument1_PrintPage;

PrintDocument1.Print();

}



}






catch(Exceptionex)


{






MessageBox.Show(ex.Message.ToString(), "Error");


}

}






publicvoidPreviewToolStripMenuItem_Click(objectsender, System.EventArgse)


{






try



{






PrintDialogprintDialog = newPrintDialog();


PrintPreviewDialog1.Document = PrintDocument1;

PrintPreviewDialog1.ShowDialog();






if(printDialog.ShowDialog() == DialogResult.OK)


{






StringReaderreader = newStringReader(rtbDoc.Text);




PrintDocument1.PrintPage += PrintDocument1_PrintPage;

PrintDocument1.Print();

}



}






catch(Exceptionex)


{






MessageBox.Show(ex.Message.ToString(), "Error");


}

}






privatevoidPrintDocument1_PrintPage(objectsender, System.Drawing.Printing.PrintPageEventArgse)


{








StringReaderreader = newStringReader(rtbDoc.Text);







floatLinesPerPage = 0;







floatYPosition = 0;







intCount = 0;







floatLeftMargin = e.MarginBounds.Left;







floatTopMargin = e.MarginBounds.Top;







stringLine = null;







FontPrintFont = this.rtbDoc.Font;







SolidBrushPrintBrush = newSolidBrush(Color.Black);


LinesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics);






while(Count < LinesPerPage && ((Line = reader.ReadLine()) != null))


{

YPosition = TopMargin + (Count * PrintFont.GetHeight(e.Graphics));

e.Graphics.DrawString(Line, PrintFont, PrintBrush, LeftMargin, YPosition,




newStringFormat());


Count++;

}

















if(Line != null)


{

e.HasMorePages =




true;


}






else



{

e.HasMorePages =




false;


}

PrintBrush.Dispose();

}


Continue reading...
 
Back
Top