C# - Result from e.Graphics.DrawString does not output what I expect

  • Thread starter Thread starter iHandler
  • Start date Start date
I

iHandler

Guest
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPos = 0;
float count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = e.MarginBounds.Height / _font.GetHeight(e.Graphics);

while (count < linesPerPage && ((line = _reader.ReadLine()) != null))
{
if (line.Contains("[page_break]"))
break;

yPos = topMargin + (count * _font.GetHeight(e.Graphics));
e.Graphics.DrawString(line, _font, Brushes.Black, leftMargin, yPos, new StringFormat());

count++;
}
// Set page count
PageCount += 1;

// If more lines exist, print another page.
if (line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}

The coding above works fine except with some lines longer than the margin (i.e. the length too long but it didn't go to next line or wordwrap), so it got cropped. I believe there is some problem for these lines

yPos = topMargin + (count * _font.GetHeight(e.Graphics));
e.Graphics.DrawString(line, _font, Brushes.Black, leftMargin, yPos, new StringFormat());

Can somebody help how to correct this problem?

thank you

Continue reading...
 
Back
Top