kasdoffe
Well-known member
- Joined
- Aug 27, 2003
- Messages
- 57
I am trying to complete some printing functionality that a friend has started. His printing and printpriview functionality use the same code below. However, when using print preview, the preview shows the first two pages printed on top of each other. Not using print preview works fine.
Any ideas why print preview shows the first 2 pages on top of each other?
Heres some more info... I am trying to print a string array with 150 lines in it. Through the first iteration thru this method, HasMorePages is set to true near the bottom, then, when breaking at the beginning of this method on the 2nd iteration, it is still set to true. This is the page that is reprinted on top of the 1st page. At the end of the 2nd iteration, it is set to true again to print a 3rd page. However, breaking at the beginning of the 3rd iteration, HasMorePages is now false and it actually prints another page correctly. The end result is this. The first page is actually the first 66 lines with the next 66 lines printed on top of them. The 2nd page is the rest of the lines.
Any ideas why print preview shows the first 2 pages on top of each other?
Heres some more info... I am trying to print a string array with 150 lines in it. Through the first iteration thru this method, HasMorePages is set to true near the bottom, then, when breaking at the beginning of this method on the 2nd iteration, it is still set to true. This is the page that is reprinted on top of the 1st page. At the end of the 2nd iteration, it is set to true again to print a 3rd page. However, breaking at the beginning of the 3rd iteration, HasMorePages is now false and it actually prints another page correctly. The end result is this. The first page is actually the first 66 lines with the next 66 lines printed on top of them. The 2nd page is the rest of the lines.
Code:
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
if (linesprinted < numlines)
{
printFont = new Font(FD.Font.FontFamily.Name,FD.Font.Size);
ev.HasMorePages = false;
count = 0;
yPos = ev.PageSettings.Margins.Top;
ev.PageSettings.Margins.Left = storedPageSettings.Margins.Left;
ev.PageSettings.Margins.Top = storedPageSettings.Margins.Top;
ev.PageSettings.Margins.Right = storedPageSettings.Margins.Right;
ev.PageSettings.Margins.Bottom = storedPageSettings.Margins.Bottom;
lpp = (ev.PageBounds.Height - ev.PageSettings.Margins.Bottom) / printFont.GetHeight();
while (count < (lpp - 1) && count <= linesToPrint && yPos <= (ev.PageSettings.PaperSize.Height - ev.PageSettings.Margins.Bottom))
{
line="";
if (printBuffer[linesprinted]!=null) line = printBuffer[linesprinted].ToString();
line = line.Replace("\f","");
if (count > 0) yPos = ev.PageSettings.Margins.Top + (count * printFont.GetHeight());
if (yPos <= (ev.PageSettings.PaperSize.Height - ev.PageSettings.Margins.Bottom))
{
ev.Graphics.DrawString(line,printFont,Brushes.Black,ev.PageSettings.Margins.Left,yPos,new StringFormat());
count++;
linesprinted++;
}
}
//If we have more lines then print another page
if (line != null && linesprinted < numlines)
{
ev.HasMorePages = true ;
if (linesToPrint > lpp) linesToPrint = linesToPrint - count;
}
else ev.HasMorePages = false;
}
}
Last edited by a moderator: