Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program generates barcodes , so i have an issue when it comes to print the next page. The preview doesnt show the next page and stops . Can someone please help me.. I have a for loop so when i check the page bounds and set the hasmorepages value true , the preview doesnt stop generating previews .

What I have tried:

for (int i = 0; i < second; i++)
               {
                  // MessageBox.Show(c + " c value " + p + " p value");
                   if (c < Int32.Parse(textBoxY.Text)) {

                       for (o = 0; o < first; o++)
                       {
                           {

                               if (textBoxX.Text == "1" & comboBox3.SelectedIndex != 4) { h += placing1_h(); }//1
                               else if (textBoxX.Text == "1" & comboBox3.SelectedIndex == 4) { h += placing1_h(); }//1

                               if (comboBox3.SelectedIndex == 3 & textBoxX.Text == "2") { h += 40; }
                               if (comboBox3.SelectedIndex == 4 & textBoxX.Text == "2") { h += 30; }
                               if (comboBox3.SelectedIndex == 2 & textBoxX.Text == "2") { h += 60; }
                               if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "2") { h += 80; }
                               if (comboBox3.SelectedIndex == 1 & textBoxX.Text == "2") { h += 60; }
                               if (comboBox3.SelectedIndex == 1 & textBoxX.Text == "3") { h += 20; }
                               if (comboBox1.SelectedIndex == 3 && textBoxX.Text == "3" && comboBox3.SelectedIndex == 1) { h -= 5; }
                               if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "3") { h += 20; }
                               if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "5") { h+=5; }
                               e.HasMorePages = true;

                           e.Graphics.DrawString(textBoxResult.Lines[c].ToString(), font, Brushes.Black, new Point(h + mikos, p));
                           ///MessageBox.Show(p+" --- ");
                           if (e.HasMorePages = p > e.PageBounds.Bottom)
                           {
                               e.HasMorePages = true;
                               break;
                           }

                           if ((comboBox3.SelectedIndex == 2 & textBoxX.Text == "3")) { h += placing3_h(); }//2
                               if ((comboBox3.SelectedIndex == 2 & textBoxX.Text == "4")) { h += placing4_h(); }//2
                               if ((comboBox3.SelectedIndex == 2 & textBoxX.Text == "2")) { h += placing2_h(); }//2

                               if (comboBox3.SelectedIndex == 3 & textBoxX.Text == "3") { h += placing3_h(); }//3
                               if (comboBox3.SelectedIndex == 3 & textBoxX.Text == "2") { h += placing2_h(); }//γινεται υπερκαλυψη δεν μπορει να χωρεσει αλλο

                               if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "2") { h += placing2_h(); }//0
                               if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "3") { h += placing3_h(); }//0
                               if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "4") { h += placing4_h(); }//0
                               if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "5") { h += 160; }//0

                               if (comboBox3.SelectedIndex == 4 & textBoxX.Text == "2") { h += placing2_h(); }//4
                               if (comboBox3.SelectedIndex == 4 & textBoxX.Text == "3") { h += placing3_h(); }//4

                               if (comboBox3.SelectedIndex == 1 & textBoxX.Text == "2") { h += placing2_h(); }
                               if (comboBox3.SelectedIndex == 1 & textBoxX.Text == "4") { h += placing4_h(); }
                               if (comboBox3.SelectedIndex == 1 & textBoxX.Text == "3") { h += placing3_h(); }//1
                               c++;
                       }
                   }

                   }
                   if (comboBox3.SelectedIndex == 0 & textBoxX.Text == "5") { h = 10; }
                   else
                   {
                       h = 20;
                   }
                   p += 100 + height;

                   e.HasMorePages = false;
               }


private void preview(PrintDocument doc)
        {
            try
            {
                PrintPreviewDialog printDialog = new PrintPreviewDialog();
                printDialog.Document = doc;
                printPreviewDialog1.PrintPreviewControl.StartPage = 1;
                printPreviewDialog1.PrintPreviewControl.Columns = 3;
               // doc.EndPrint += doc_EndPrint; // Subscribe to EndPrint event of your document here.
                printDialog.ShowDialog();

            }
            catch (System.ArgumentException ex) { }
        }
Posted
Updated 7-May-19 2:03am
Comments
Maciej Los 28-Apr-19 14:52pm    
I'd change
if (e.HasMorePages = p > e.PageBounds.Bottom)
{
e.HasMorePages = true;
break;
}

with:
if (p > e.MarginBounds.Bottom)
e.HasMorePages = true;
Member 12221464 28-Apr-19 14:59pm    
Still no results ... Do you want to send you the project ?
Maciej Los 28-Apr-19 15:04pm    
Please, don't.
You have to debug your programme to find out where the problem is...
Member 12221464 29-Apr-19 12:30pm    
The problem is that the print method doesnt continue to generate barcodes on the next page so i only see the first one ...

this is the code from the method that sends information to preview
doc.PrintPage += Doc_PrintPage2;

pd.AllowSelection = true;
pd.AllowSomePages = true;
pd.Document = doc;
preview(doc);

This is the if statement which i placed inside the for loop after the e.Graphics.DrawString
if (p < e.MarginBounds.Bottom)

{
e.HasMorePages = false;
i += 1;
}
else
{
i = 0;
e.HasMorePages = true;
return;
}
Maciej Los 29-Apr-19 14:40pm    
Check your logic:

if (p < e.MarginBounds.Bottom)

{
e.HasMorePages = false;
i += 1;
}
else

As to me, it should be:

if (p > e.MarginBounds.Bottom)

{
e.HasMorePages = true;
i += 1;
}
else

Take a look here: Printing and Previewing multiple pages in C#[^]. It may help you to understand where you've made a mistake.

The most important thing is that you have to define the number of items per page. In case when the numer of items per page will achieve desired value, you have to reset that variable, set e.HasMorePage to true and use return keyword to call page print event again. See:
C#
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
     {
          float  currentY = 10;// declare  one variable for height measurement
          e.Graphics.DrawString("Print in Multiple Pages", DefaultFont, Brushes.Black, 10, currentY);//this will print one heading/title in every page of the document
          currentY += 15;

         while(totalnumber <= 50) // check the number of items
          {
          e.Graphics.DrawString(totalnumber.ToString(),DefaultFont, Brushes.Black, 50,currentY);//print each item
          currentY += 20; // set a gap between every item
          totalnumber += 1; //increment count by 1
          if(itemperpage < 20) // check whether  the number of item(per page) is more than 20 or not
           {
             itemperpage += 1; // increment itemperpage by 1
             e.HasMorePages = false; // set the HasMorePages property to false , so that no other page will not be added

            }

           else // if the number of item(per page) is more than 20 then add one page
            {
             itemperpage = 0; //initiate itemperpage to 0 .
             e.HasMorePages = true; //e.HasMorePages raised the PrintPage event once per page .

             //!!!HERE!!!
             return;//It will call PrintPage event again

             }
          }
     }
 
Share this answer
 
The solution is right here . This is a string that generates numbers from the top to the bottom . Then it continues to the other page . So i set the brush to transparent and the barcodes exist on the other print page. Thank everyone for their advice .
doc.PrintPage += new PrintPageEventHandler (this.Doc_PrintPage);                   
                    doc.PrintPage += delegate (object sender1, System.Drawing.Printing.PrintPageEventArgs e1)
                {
                    float size = (float)0;
                    int charsFitted, linesFilled;
                    Font f = null ;
                    try
                    {
                        if (textBoxX.Text == "1")
                        {
                            size = 64;
                        }
                        else if (textBoxX.Text == "2")
                        {
                            size = 33;
                        }
                        else if (textBoxX.Text == "3")
                        {
                            size = 22;
                        }
                        else if (textBoxX.Text == "4")
                        {
                            size =(float) 15.8;
                        }
                        else if (textBoxX.Text == "5")
                        {
                            size =(float) 13.2;
                        }
                        f = new Font("Arial", size);
                    }
                    catch (System.ArgumentException ep) { }                   
                        var realsize = e1.Graphics.MeasureString(
                        textBoxResult.Text,
                        f,
                        e1.MarginBounds.Size, 
                        System.Drawing.StringFormat.GenericDefault,
                        out charsFitted,  
                        out linesFilled);

                var fitsOnPage =  textBoxResult.Text.Substring(0, charsFitted);
                textBoxResult.Text = textBoxResult.Text.Substring(charsFitted).Trim().ToString();

                e1.Graphics.DrawString(
                        fitsOnPage,
                        f,
                        Brushes.Transparent,
                        e1.MarginBounds); 
                    e1.HasMorePages = textBoxResult.Text.Length >1;
                
                };                             
 
Share this answer
 
Comments
Richard MacCutchan 7-May-19 9:05am    
catch (System.ArgumentException ep) { }

Don't do that. If an exception gets thrown you, or worse your user, will never know why the application is not working properly.
Maciej Los 7-May-19 9:56am    
You should say "Thanks" by accepting valuable answers and using voting system (stars in the right-top corner of solution: 1 and 2 -the downvote, 3 - neutral, 4 and 5 - upvote).
+5 for your effort.

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