Help with Print preview!

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.

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:
Sorry if this answer comes too late, but I too have been having problems with this. Mine, however was happening during actual printing, not just on the screen. Turns out it was setting HasMorePages and then continuing execution in the same routine.

Mainly the best way I have found to describe this is that the .HasMorePages property is a "please call me again" request.
The best way to work around it is to either do most of your printing formatting and such outside of the PrintPage routine and then access the information from the routine. Or, if this is not as feesable of a choice, as with my project, then you could have the code keep track of where you are at and do a resume type of function.

For instance, my code loops through a database, printing labels with record details on each. As it loops, it sets a variable to the current records ID field, and then when the statement that knows its the last record fires, it sets HasMorePages and exits the sub, which makes .NET call it again fresh. The code then grabs the variable and resumes from that record.

Hope this helps.

XPToaster
Freshly toasted .NET applications.
 
Back
Top