printing text

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



Answer this question

printing text

  • Sajidsr

    I finally got NET2.0 installed and tried the code listed. It doesn't throw any errors and I have found that if I remove the line that sets the margins everything works fine (even though the top and bottom margins are bigger than my settings).
  • mrbopalop1

    did you remove the -4 from the lpp calculation before trying my listing
  • Nikolas134886

    PrintableArea is new to .NET 2.0

  • 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

    Yep, I removed the "-4" from the lpp calculation and didn't get any missing lines.

  • tojo

    Thanks for all the help with this. I think I understand what your getting at here. I am just going to have to experiment a little to get my head around it!
  • flynng

    How are you drawing the text to the printer If you're adding any spacing between the lines your calculation won't be accurate.

  • Michael AG

    I tried using the above code but it fails to build - telling me there is no definition for 'PrintableArea'. Do you think I am missing a namespace (I am using DotNet 1.1.
  • MouleeswaranSwaminathan

    There's page size (like 8.5x11 inches) and page margins. Then, there's the area of the page that the printer can actually print to, which varies from printer to printer. On my Brother printer, the printable area margin seems to be .12 inches all around. On my HP DeskJet, it seems to be .13, .63, .25, .25 inches (top, bottom, left, right).

  • 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));

    What's probably happening is you're setting your margins so they're outside the printable area of your printer (see the PrintableArea property). The above will assert if you set margins that will create a page rectangle that doesn't fit within the printable area for the selected printer.



  • rima66345

    I haven't had time to install VS2005 yet but will try your code when I get it installed and let you know what happens. I am trying to understand how the margins can be outside the print area. I thought the margins worked inwards from the edge of the page (or is this something to do with the page size being bigger than the printers hard size ).
  • 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 ;

    else

    ev.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 point

    cResultLine = 0;

    printFont = new Font("Courier New", 9);

    PrintDocument pd = new PrintDocument(); //Assumes the default printer

    Margins 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);

    }

    }


  • printing text