کد:
private void printToolStripButton_Click(object sender, EventArgs e)
{
try
{
op = new OpenFileDialog();
op.ShowDialog();
//MessageBox.Show(op.FileName);
filePath = op.FileName;
streamToPrint = new System.IO.StreamReader(filePath);
printFont = new System.Drawing.Font("Tahoma", 8);
System.Drawing.Printing.PrintDocument PD = new System.Drawing.Printing.PrintDocument();
PD.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PD_PrintPage);
PD.Print();
try
{
}
finally
{
streamToPrint.Close();
}
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Error");
}
}
private void PD_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
String line = null;
// Number Of Lines
linesPerPage = e.MarginBounds.Height;
printFont.GetHeight(e.Graphics);
// Printing Each line
while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black, leftMargin, topMargin, new StringFormat());
count++;
}
// If More Line Exist
if (line != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}