且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何打印多个页面C#

更新时间:2023-02-12 21:54:05

看看这里:在C#中打印和预览多个页面 [ ^ ]。它可以帮助你理解你犯了什么错误。



最重要的是你必须定义每页的项目数。如果每页的项目数量达到所需值,则必须重置该变量,将 e.HasMorePage 设置为 true 并使用 return 关键字再次调用页面打印事件。请参阅:

  private   void  printDocument1_PrintPage(  object  sender,System.Drawing.Printing.PrintPageEventArgs e)
{
float currentY = 10 ; // 声明一个高度测量变量
e.Graphics.DrawString( 在多个页面中打印,DefaultFont ,Brushes.Black, 10 ,currentY); // 这将在文档的每个页面中打印一个标题/标题
currentY + = 15 ;

while (totalnumber < = 50 // 检查项目数
{
e.Graphics.DrawString(totalnumber.ToString(),DefaultFont,Brushes.Black, 50 ,currentY); / / 打印每个项目
currentY + = 20 ; // 设置每个项目之间的差距
totalnumber + = 1 跨度>; // 增量计数为1
if (itemperpage < 20 // 检查商品数量(每页)是否超过20
{
itemperpage + = 1 跨度>; // 将itemperpage增加1
e.HasMorePages = 假跨度>; // 将HasMorePages属性设置为false,以便不会添加其他页面

}

其他 // 如果项目数(每页)超过20,则添加一页
{
itemperpage = 0 ; // 将itemperpage设为0。
e.HasMorePages = 真跨度>; // e.HasMorePages每页引发一次PrintPage事件。

//
!!! HERE !!!
返回; // 它将再次调用PrintPage事件

}
}
}


解决方案就在这里。这是一个从顶部到底部生成数字的字符串。然后它继续到另一页。所以我将画笔设置为透明,条形码存在于另一个打印页面上。感谢大家的建议。

 doc.PrintPage + = new PrintPageEventHandler(this.Doc_PrintPage); 
doc.PrintPage + = delegate(object sender1,System.Drawing.Printing.PrintPageEventArgs e1)
{
float size =(float)0;
int charsFitted,linesFilled;
字体f = null;
尝试
{
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 =新字体(Arial,大小);
}
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;

};


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) { }
        }

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:
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

             }
          }
     }


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;
                
                };