Print Document PrintPage problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mischief07
    New Member
    • Apr 2010
    • 4

    Print Document PrintPage problem

    I am having an issue with printing a text file that has multiple pages. The file is approx 19 pages but when I call printpage it will print page one and two on the same page with the text overlapping. this happens for page 3 and 4, 5 and 6 etc.

    here is my code for when the print button is pressed

    StreamReader sr is a global variable

    Code:
               
                streamFilename = "PullSummary.txt";
                sr = new StreamReader(streamFilename);
                
                pd.DocumentName = streamFilename;
    
                pd.PrinterSettings.PrinterName = "\\\\mintoms02\\Canon5000i";
                pd.PrinterSettings.DefaultPageSettings.Margins.Top = 10;
                pd.PrinterSettings.DefaultPageSettings.Margins.Bottom = 10;
                pd.PrinterSettings.DefaultPageSettings.Margins.Right = 10;
                pd.PrinterSettings.DefaultPageSettings.Margins.Left = 10;
                //pd.PrinterSettings.
                pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                pd.Print();
    Here is my PrintPage Event Handler

    Code:
    private void pd_PrintPage(object sender, PrintPageEventArgs e)
            {
                float linesPerPage = 0;
                float yPos = 0;
                int count = 0;
                float leftMargin = 10;
                float topMargin = 10;
                string line = null;
                // Calculate the number of lines per page.
                linesPerPage = e.PageBounds.Height / printFont.GetHeight(e.Graphics);
    
                line = sr.ReadLine();
                // Print each line of the file.
                while (count < linesPerPage-1 && (line != null))
                {
                    yPos = topMargin + (count *  printFont.GetHeight(e.Graphics));
                    e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                    count++;
                    line = sr.ReadLine();
                }
                yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
                e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
    
                // Check if any more pages left for printing
                if (line != null)
                {
                    e.HasMorePages = true;
                }
                else
                {
                    e.HasMorePages = false;
                    sr.Close();
                }
    
            }
    If I step through the code a dialog box "Printing Page 1" pops up as soon as the PrintPage event is raised. It will then go through sr correctly and the hasmorepages will be set to true. The second time through the dialog box with still say "Printing Page 1". It is not until the third time through that the dialog box will change to say "Printing Page 2". I have tried different printers with the same result.

    Any ideas would be greatly appreciated.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I *think* this is the problem
    Code:
    linesPerPage = e.PageBounds.Height / printFont.GetHeight(e.Graphics);
    Try getting the height of a capital M. That will be a full height character for a correct height of each line.

    A lot of your calculations are based on getting the height of e.Graphics. May I suggest you set the height value to a variable at one point at the start of your routine, then use this variable throughout.

    This way you can test by hardcoding values until you know approximately what value works. Then you can keep tweeking your calculation.

    Code:
    string line = null;
    //int LineHeight = 5; // Nope, too small
    // int LineHeight = 15; // Still too small
    // int LineHeight = 75; // Way too big
    // int LineHeight = 35; // Getting closer
    int LineHeight = 30; // Not bad.  This is what my formula need to get close to
    
    LineHeight = printFont.GetHeight('M');
    if (UserWantsDoubleSpace) LineHeight *=2;
    
                // Calculate the number of lines per page.
                linesPerPage = e.PageBounds.Height / LineHeight;

    Comment

    • Mischief07
      New Member
      • Apr 2010
      • 4

      #3
      No this is not the issue. linesPerPage is getting the correct number of lines (69). Even if I hardcode linesPerPage to be 20, I get 20 lines of 2 pages of text overlapped on one page.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Right. But if your measurement of line HEIGHT is wrong, then your spacing calculation is wrong.

        Code:
        yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
                    e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
        Here again you are trying to calculate the new line's position off of GetHeight(e.Gra phics)

        If your line height is 2 instead of 20 then you will calculate a new yPos that overlaps the old yPos - which sounds likes the behavior you are getting.

        Comment

        • Mischief07
          New Member
          • Apr 2010
          • 4

          #5
          Sorry about the misunderstandin g. I tried other lineheingts with no success.

          I double checked the line height and printFont.GetHe ight(e.Graphics ) does return the correct line height. I can see the first line being added to the printing page all the way to the 69th line. The function then sets e.HasMorePages to true and returns to print a new page. The issue is here, for some reason even though PrintPage gets called again it's not telling the printer that we are on a new page. The second page then overlaps perfectly ontop of the first, I can also tell by physically looking at the page. The third page will start on a new page but the fourth will overlap the third. The overlapping happens on all even pages.

          I've also tried many different printers with no success.

          Thank you very much for the prompt suggesstions so far.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            I completely misunderstood the issue. Hard to visualize. I thought you were having line spacing problems where line2 was overlapping line1... not page2 overlapping page1

            Could it be this
            Code:
            wwhile (count < linesPerPage-1 && (line != null))
                        {
                            yPos = topMargin + (count *  printFont.GetHeight(e.Graphics));
                            e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                            count++;
                            line = sr.ReadLine();
                        }
                        yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
            Do you really need to ignore null lines? Do blank lines that are just C/R get counted as null? Are you overlapping by exactly the number of blank lines (between paragraphs)? I'm just thinking the counter only gets incremented on non-blank lines instead of all lines.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              I completely misunderstood the issue. Hard to visualize. I thought you were having line spacing problems where line2 was overlapping line1... not page2 overlapping page1

              Could it be this
              Code:
              wwhile (count < linesPerPage-1 && (line != null))
                          {
                              yPos = topMargin + (count *  printFont.GetHeight(e.Graphics));
                              e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                              count++;
                              line = sr.ReadLine();
                          }
                          yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
              Do you really need to ignore null lines? Do blank lines that are just C/R get counted as null? Are you overlapping by exactly the number of blank lines (between paragraphs)? I'm just thinking the counter only gets incremented on non-blank lines instead of all lines.

              Comment

              • Mischief07
                New Member
                • Apr 2010
                • 4

                #8
                There are no blank lines in the text file. It is a dumb of data from a database that I do before printing. here is a small portion of it.

                Code:
                --------------------2010-04-27 Kanban A5D3--------------------
                Pull #3 A5D3: AA 16  3 of 6 -- 2010-04-28 10:30 AM
                Pull #6 A5D3: AA 16  4 of 6 -- 2010-04-28 10:30 AM
                Pull #9 A5D3: AA 16  5 of 6 -- 2010-04-28 10:30 AM
                Pull #12 A5D3: AA 16  6 of 6 -- 2010-04-28 10:30 AM
                Pull #15 A5D3: AA 17  1 of 3 -- 2010-04-28 10:30 AM
                Pull #18 A5D3: AA 17  2 of 3 -- 2010-04-28 10:30 AM
                Pull #21 A5D3: AA 17  3 of 3 -- 2010-04-28 10:30 AM
                Pull #24 A5D3: AA 18  1 of 6 -- 2010-04-28 10:30 AM
                Pull #27 A5D3: AA 18  2 of 6 -- 2010-04-28 10:30 AM
                Pull #30 A5D3: AA 18  3 of 6 -- 2010-04-28 10:30 AM
                Pull #33 A5D3: AA 18  4 of 6 -- 2010-04-28 10:30 AM
                Pull #36 A5D3: AA 18  5 of 6 -- 2010-04-28 10:30 AM
                Pull #39 A5D3: AA 18  6 of 6 -- 2010-04-28 10:30 AM
                Pull #42 A5D3: AA 19  1 of 6 -- 2010-04-28 10:30 AM
                Pull #45 A5D3: AA 19  2 of 6 -- 2010-04-28 10:30 AM
                Pull #48 A5D3: AA 19  3 of 6 -- 2010-04-28 10:30 AM
                Pull #51 A5D3: AA 19  4 of 6 -- 2010-04-28 10:30 AM
                Pull #54 A5D3: AA 19  5 of 6 -- 2010-04-28 10:30 AM
                Pull #57 A5D3: AA 19  6 of 6 -- 2010-04-28 10:30 AM
                Pull #60 A5D3: AA 20  1 of 6 -- 2010-04-28 10:30 AM
                Pull #63 A5D3: AA 20  2 of 6 -- 2010-04-28 10:30 AM
                Pull #85 A5D3: AA 20  3 of 6 -- 2010-04-28 10:30 AM
                Pull #88 A5D3: AA 20  4 of 6 -- 2010-04-28 10:30 AM
                Pull #91 A5D3: AA 20  5 of 6 -- 2010-04-28 10:30 AM
                Pull #94 A5D3: AA 20  6 of 6 -- 2010-04-28 10:30 AM
                --------------------2010-04-27 Kanban A718--------------------
                Pull #93 A718: AA 13  1 of 1 -- 2010-04-28 10:30 AM
                Pull #95 A718: AA 16  1 of 1 -- 2010-04-28 10:30 AM
                Pull #96 A718: AA 19  1 of 1 -- 2010-04-28 10:30 AM
                --------------------2010-04-27 Kanban C577--------------------
                Pull #15 C577: 1R D14 1 of 1 -- 2010-04-27 4:50 PM
                Pull #63 C577: 1R D01 1 of 1 -- 2010-04-28 8:00 AM
                Pull #95 C577: 1R D11 1 of 1 -- 2010-04-28 1:50 PM
                --------------------2010-04-27 Kanban C598--------------------
                Pull #2 C598: 2A D10 4 of 5 -- 2010-04-27 11:45 AM
                Pull #5 C598: 2A D10 5 of 5 -- 2010-04-27 11:45 AM
                Pull #9 C598: 2A D14 1 of 6 -- 2010-04-27 2:10 PM
                Pull #11 C598: 2A D14 2 of 6 -- 2010-04-27 2:10 PM
                Pull #13 C598: 2A D14 3 of 6 -- 2010-04-27 2:10 PM

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  I'm out of idea then. Sorry

                  Comment

                  Working...