I am trying to print a text file and am using the examples provided by Visual Studio. The following line calculates the number of lines per page in the PrintPage eventhandler:
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
The problem is that lpp calculates a number of lines but the printer is printing past the margins and I miss 2-3 lines per page. If I subtract 4 from lpp this works but why isn't the calculation correct

printing text
Sajidsr
mrbopalop1
Nikolas134886
Lord Raoul
Apparently you have to use CreateMeasurementGraphics() to get the device capabilities of the printer to find out the printable area in .NET 1.1
There's an example of doing this in C# at http://discuss.fogcreek.com/joelonsoftware1/default.asp cmd=show&ixPost=32572 (use of the hdc after ReleaseHdc seems suspect in this example)
and VB.NET at http://dotnet.mvps.org/dotnet/faqs/ id=getphysicalprintermargins&lang=en
You should also read http://seewinapp.blogspot.com/2005/10/avoid-leaking-from-printersettings-net.html with regards to possible leaks when using CreateMeasurementGraphics() in .NET 1.1
quicksun
tojo
flynng
Michael AG
MouleeswaranSwaminathan
Mars_Ram
What happens when you add the following code after setting pd.DefaultPageSettings.Margins:
RectangleF margins = new RectangleF(pd.PrinterSettings.DefaultPageSettings.Margins.Left
, pd.PrinterSettings.DefaultPageSettings.Margins.Top
, pd.PrinterSettings.DefaultPageSettings.Bounds.Width - (pd.PrinterSettings.DefaultPageSettings.Margins.Left + pd.PrinterSettings.DefaultPageSettings.Margins.Right)
, pd.PrinterSettings.DefaultPageSettings.Bounds.Height - (pd.PrinterSettings.DefaultPageSettings.Margins.Top + pd.PrinterSettings.DefaultPageSettings.Margins.Bottom));
System.Diagnostics.Debug.Assert(pd.PrinterSettings.DefaultPageSettings.PrintableArea.Contains(margins));
rima66345
M. Hassan Raza
Your code worked fine for me. Are you changing the printer settings before printing
Does this work for you :
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float fontHeight = printFont.GetHeight(e.Graphics);
int lpp = (int)(e.MarginBounds.Height / fontHeight);
StringFormat stringFormat = new StringFormat();
for (int i = 1; i <= lpp; ++i)
{
e.Graphics.DrawString(i.ToString() + "/" + lpp.ToString()
, printFont
, Brushes.Black
, e.MarginBounds.Left
, e.MarginBounds.Top + (i * fontHeight));
}
}
HelenZHOU
The event is listed below:
//Event fired for each page to print private void pd_PrintPage(object sender, PrintPageEventArgs ev){
float lpp = 0 ; float yPos = 0 ; int count = 0 ; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; //Work out the number of lines per page //Use the MarginBounds on the event to do this float test = ev.PageBounds.Height;lpp = (ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics))-4;
//Now iterate over the results printing out each line //NOTE WELL: This assumes that a single line is not wider than the page width //Check count first so that we don't read line that we won't print while (count < lpp && cResultLine < txtResults.Lines.Length){
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
//Print Preview control will not work.ev.Graphics.DrawString (txtResults.Lines[cResultLine], printFont, Brushes.Black, leftMargin,
yPos,
new StringFormat());cResultLine++;
count++;
}
//If we have more lines then print another page if (cResultLine < txtResults.Lines.Length)ev.HasMorePages =
true ; elseev.HasMorePages =
false ;}
this event is printing the contents of a rich text box to a printer. It uses lpp to count the number of lines on a page and keeps an eye on the number of lines in the rich text box. I originally wondered whether the conversion between float and int was a problem but it looked correct and shouldn't account for more than one line difference.
Tim Attaway
The event is defined in the following routine - do you think it may be the font type
//print the contents of the editor window private void mnuFPrint_Click(object sender, System.EventArgs e){
try{
//initialise the start pointcResultLine = 0;
printFont =
new Font("Courier New", 9);PrintDocument pd =
new PrintDocument(); //Assumes the default printerMargins margins =
new Margins(30,25,25,25);pd.DefaultPageSettings.Margins = margins;
pd.PrintPage +=
new PrintPageEventHandler(this.pd_PrintPage);pd.Print();
}
catch{
MessageBox.Show("error printing","warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}