Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to do multipage print. However, it only print one page. How can I solve this problem? Please check my code and let me know which i made mistake.
C#
private void Print_Form(DataSet dsPRTSET)
{
   dsPRTDB = dsPRTSET;
   PrintDocument pfDoc = new PrintDocument();
   PrintPreviewDialog prPRVVIEW = new PrintPreviewDialog();
   pfDoc.DefaultPageSettings.Landscape = false;
   pfDoc.DefaultPageSettings.PaperSize = new PaperSize("PaperLetter", 850, 1100);
   pfDoc.PrintPage += new PrintPageEventHandler(this.PF_PRINTPAGE);
   prPRVVIEW.Document = pfDoc;
   prPRVVIEW.Show();
}
private void PF_PRINTPAGE(object sender, PrintPageEventArgs e)
{
   float nprLines = 0;
   float nYPos = 0;
   float nTopMargin = e.MarginBounds.Top;
   float nLeftMargin = e.MarginBounds.Left;
   Font prtFont = new Font("Courier New",10);
   nprLines = e.MarginBounds.Height / prtFont.GetHeight(e.Graphics);
   string cTMVAL = "";
   Brush MyBrush = new SolidBrush(Color.Black);
   System.Drawing.Font MyFont = new System.Drawing.Font("Courier New", 10);
   Pen MyPen = new Pen(MyBrush, 0.1F);
   e.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
   Graphics prtG = e.Graphics;

   string cSEASON = "";
   if (opnSEASON1.Checked == true)
   {
      cSEASON = "부활";
   }
   else
   {
      cSEASON = "성탄";
   }

   int ml = 0;
   int cl = 0;
   for (ml = 0; ml <= dsPRTDB.Tables[0].Rows.Count - 1; ml++)
   {
      switch (cl)
      {
         case 0:
         case 2:
         case 4:
            if (cl == 0)
            {
               cTMVAL = txtYEAR.Text + " 년 " + cSEASON + " 판공성사표";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 20F);
               cTMVAL = "NO: " + dsPRTDB.Tables[0].Rows[ml]["PP_NO"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 25F);
               cTMVAL = "이름: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_HNAME"].ToString() +
                        "(" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_FNAME"].ToString() + " " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_LNAME"].ToString() + ")";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 35F);
               cTMVAL = "세례명: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_SNAME"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 40F);
               cTMVAL = "구역/반 -> " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_KUYOUK"].ToString() + "/" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_BAN"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 45F);
            }
            else if (cl == 2)
            {
               cTMVAL = txtYEAR.Text + " 년 " + cSEASON + " 판공성사표";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 85F);
               cTMVAL = "NO: " + dsPRTDB.Tables[0].Rows[ml]["PP_NO"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 90F);
               cTMVAL = "이름: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_HNAME"].ToString() +
                        "(" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_FNAME"].ToString() + " " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_LNAME"].ToString() + ")";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 100F);
               cTMVAL = "세례명: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_SNAME"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 105F);
               cTMVAL = "구역/반 -> " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_KUYOUK"].ToString() + "/" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_BAN"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 110F);
            }
            else
            {
               cTMVAL = txtYEAR.Text + " 년 " + cSEASON + " 판공성사표";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 140F);
               cTMVAL = "NO: " + dsPRTDB.Tables[0].Rows[ml]["PP_NO"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 145F);
               cTMVAL = "이름: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_HNAME"].ToString() +
                        "(" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_FNAME"].ToString() + " " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_LNAME"].ToString() + ")";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 155F);
               cTMVAL = "세례명: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_SNAME"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 160F);
               cTMVAL = "구역/반 -> " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_KUYOUK"].ToString() + "/" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_BAN"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 13F, nYPos + 165F);
            }
            break;
         case 1:
         case 3:
         case 5:
            if (cl == 1)
            {
               cTMVAL = txtYEAR.Text + " 년 " + cSEASON + " 판공성사표";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 20F);
               cTMVAL = "NO: " + dsPRTDB.Tables[0].Rows[ml]["PP_NO"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 25F);
               cTMVAL = "이름: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_HNAME"].ToString() +
                        "(" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_FNAME"].ToString() + " " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_LNAME"].ToString() + ")";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 35F);
               cTMVAL = "세례명: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_SNAME"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 40F);
               cTMVAL = "구역/반 -> " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_KUYOUK"].ToString() + "/" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_BAN"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 45F);
            }
            else if (cl == 3)
            {
               cTMVAL = txtYEAR.Text + " 년 " + cSEASON + " 판공성사표";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 85F);
               cTMVAL = "NO: " + dsPRTDB.Tables[0].Rows[ml]["PP_NO"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 90F);
               cTMVAL = "이름: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_HNAME"].ToString() +
                        "(" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_FNAME"].ToString() + " " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_LNAME"].ToString() + ")";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 100F);
               cTMVAL = "세례명: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_SNAME"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 105F);
               cTMVAL = "구역/반 -> " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_KUYOUK"].ToString() + "/" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_BAN"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 110F);
            }
            else
            {
               cTMVAL = txtYEAR.Text + " 년 " + cSEASON + " 판공성사표";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 140F);
               cTMVAL = "NO: " + dsPRTDB.Tables[0].Rows[ml]["PP_NO"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 145F);
               cTMVAL = "이름: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_HNAME"].ToString() +
                        "(" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_FNAME"].ToString() + " " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_LNAME"].ToString() + ")";
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 155F);
               cTMVAL = "세례명: " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_SNAME"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 160F);
               cTMVAL = "구역/반 -> " +
                        dsPRTDB.Tables[0].Rows[ml]["PP_KUYOUK"].ToString() + "/" +
                        dsPRTDB.Tables[0].Rows[ml]["PP_BAN"].ToString();
               prtG.DrawString(cTMVAL, MyFont, MyBrush, 110F, nYPos + 165F);
            }
            break;
      }
      cl++;
      if (cl > 5)
      {
         e.HasMorePages = true;
         cl = 0;
         nYPos += 255F;
      }
   }
   e.HasMorePages = false;
   dsPRTDB.Dispose();
}
Posted
Updated 20-Jul-11 13:26pm
v2

It prints only one page because you have
e.HasMorePages = false; at the end.

Set it to false only when you're done with your print job.
 
Share this answer
 
Comments
Dong Kim 21-Jul-11 12:14pm    
if you look at the for loop, you can see e.HasMorePages = true. I make fale after loop is done.
Alex Perepletov 21-Jul-11 14:41pm    
PrintDocument raises the PrintPage event repeatedly, as long as the previous call to PrintPage handler returns e.HasMorePages == true. Setting e.HasMorePages inside the loop has no effect on the number of calls to the PrintPage. Only the last 'e.HasMorePages = false' counts. You should change your code, - make 'cl' member variable, - at the end of the function, make 'e.HasMorePages = cl <= 5;' - do not dispose of the dsPRTDB.
Sergey Alexandrovich Kryukov 22-Jul-11 0:56am    
My 5 for spotting the problem and having patience to explain.
--SA
Dong Kim 22-Jul-11 16:28pm    
Thanks it solve one problem. but i have more to solve. It print samething over and over. Please help~
Can't nicely format comments, so I'll reply here.

Probably it prints the same thing because the for loop always starts at 0. If that's the case, make your 'ml' a member variable as well. Something like the following should work.

private int _cl;
private int _ml;

private void Print_Form(DataSet dsPRTSET)
{
...
...
     _cl = 0;
     _ml = 0;
     prPRVVIEW.Show();
}

private void PF_PRINTPAGE(object sender, PrintPageEventArgs e)
{
...
    while (_ml < dsPRTDB.Tables[0].Rows.Count)
   {
       switch (_cl)
       {
       case 0:
           DrawLine1();
           nYpos += rowHeight;
           DrawLine2();
           nYpose += rowHeight;
           break;
       case 1:
           DrawLine3();
           nYpose += rowHeight;
           break;
...
       }
      _ml++;
   }

    _cl++;
    e.HasMorePages = _cl <= 5;
}


You'll have to coordinate between line counter '_ml' and what I assume is the page counter '_cl'. Well, right now your 'for loop' is wrong because it goes throu all rows in one shot. You'll have to figure out how to break it into pages by looking at your nYpos value.

Printing is not trivial :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900