I am having an issue with printing a text file that has multiple pages. The file is approx 19 pages but when I call printpage it will print page one and two on the same page with the text overlapping. this happens for page 3 and 4, 5 and 6 etc.
here is my code for when the print button is pressed
StreamReader sr is a global variable
Here is my PrintPage Event Handler
If I step through the code a dialog box "Printing Page 1" pops up as soon as the PrintPage event is raised. It will then go through sr correctly and the hasmorepages will be set to true. The second time through the dialog box with still say "Printing Page 1". It is not until the third time through that the dialog box will change to say "Printing Page 2". I have tried different printers with the same result.
Any ideas would be greatly appreciated.
here is my code for when the print button is pressed
StreamReader sr is a global variable
Code:
streamFilename = "PullSummary.txt";
sr = new StreamReader(streamFilename);
pd.DocumentName = streamFilename;
pd.PrinterSettings.PrinterName = "\\\\mintoms02\\Canon5000i";
pd.PrinterSettings.DefaultPageSettings.Margins.Top = 10;
pd.PrinterSettings.DefaultPageSettings.Margins.Bottom = 10;
pd.PrinterSettings.DefaultPageSettings.Margins.Right = 10;
pd.PrinterSettings.DefaultPageSettings.Margins.Left = 10;
//pd.PrinterSettings.
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
Code:
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = 10;
float topMargin = 10;
string line = null;
// Calculate the number of lines per page.
linesPerPage = e.PageBounds.Height / printFont.GetHeight(e.Graphics);
line = sr.ReadLine();
// Print each line of the file.
while (count < linesPerPage-1 && (line != null))
{
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
line = sr.ReadLine();
}
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
// Check if any more pages left for printing
if (line != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
sr.Close();
}
}
Any ideas would be greatly appreciated.
Comment