-1

我正在尝试在 for 循环中打印,在该循环中必须打印组框,直到条件满足。

//代码:

private void btnPrint_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= Convert.ToInt32(lblTotalBox.Text); i++)
            {
                lblBoxNumber.Text = i.ToString();
                printDocument1.Print();
            }
        }



private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {

            PaperSize paperSize = new PaperSize("MyCustomSize", 100, 65);
            paperSize.RawKind = (int)PaperKind.Custom;
            printDocument1.DefaultPageSettings.PaperSize = paperSize;

            using (Graphics g = e.Graphics)
            {
                using (new Font("Arial", 16))
                {
                    float x = new float();
                    float y = new float();
                    x = e.MarginBounds.Left;
                    y = e.MarginBounds.Top;

                    Bitmap bmp = new Bitmap(350, 400);
                    grpReceipt.DrawToBitmap(bmp, new Rectangle(0, 0, 350, 400));
                    e.Graphics.DrawImage(bmp, x, y);

                }
            }
        }

表格图像:

有一个组框的表格只有这个要打印

当我尝试运行上面的代码时,它不会给我任何错误,但第一次打印工作正常,其他都是空的。

我哪里错了?

4

2 回答 2

1

这是你错的地方。您将printDocument1.Print()内部称为循环。

尝试这个:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        PrintDocument pd=new PrintDocument();
        int index=0, count=0;
        pd.BeginPrint+=(s, ev) =>
        {
            // find page count from form label
            count=int.Parse(label1.Text);
        };
        pd.PrintPage+=(s, ev) =>
        {
            // for each page
            index++;
            // get form size
            var size=this.Size;
            // create bitmap of same size
            var bmp=new Bitmap(size.Width, size.Height);
            // draw form into bitmap
            this.DrawToBitmap(bmp, new Rectangle(Point.Empty, size));
            // draw bitmap into graphics, resize to fit paper margins
            ev.Graphics.DrawImage(bmp, new Rectangle(ev.MarginBounds.Location, ev.MarginBounds.Size));
            // create a font and draw on graphics the page number
            using(var font = new Font(FontFamily.GenericSansSerif, 16f))
            {
                ev.Graphics.DrawString(index.ToString(), font, Brushes.Black, ev.MarginBounds.Location);
            }
            // check for final page
            ev.HasMorePages=index<count;
        };
        pd.EndPrint+=(s, ev) =>
        {
            // reset count and index
            index=0;
            count=0;
        };
        PaperSize paper=new PaperSize("MyCustomSize", 100, 65);
        paper.RawKind=(int)PaperKind.Custom;
        // set paper size
        pd.DefaultPageSettings.PaperSize=paper;
        // set paper margins appropriately
        pd.DefaultPageSettings.Margins=new Margins(10, 10, 10, 10);

        // call up the print preview dialog to see results
        PrintPreviewDialog dlg=new PrintPreviewDialog();
        dlg.Document=pd;
        dlg.ShowDialog();
    }
}

用这样一个简单的形式:

形式

唯一的按钮创建一个 10 页的文档,如下所示:

打印出

更新代码

根据下面的评论,我将打印代码更改为:

// get form size
var size=groupBox1.Size;
// create bitmap of same size
var bmp=new Bitmap(size.Width, size.Height);
// draw form into bitmap
groupBox1.DrawToBitmap(bmp, new Rectangle(Point.Empty, size));

它只抓取用于绘制的组框。

更新画面

于 2013-10-22T21:12:42.367 回答
0

我怀疑所有其他印刷品都不是空的,它们只是相互重叠。你应该这样做:

int nextTop = -1;
int i = 1;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {        
    for(;i <= Convert.ToInt32(lblTotalBox.Text); i++){
      lblBoxNumber.Text = i.ToString();
      using (Graphics g = e.Graphics) {
          int y = nextTop == -1 ? e.MarginBounds.Top : nextTop;           
          Bitmap bmp = new Bitmap(350, 400);
          grpReceipt.DrawToBitmap(bmp, new Rectangle(0, 0, 350, 400));
          g.DrawImage(bmp, e.MarginBounds.Left, y);         
          nextTop += bmp.Height + 10;
          if(nextTop > e.MarginBounds.Height - bmp.Height) {
             nextTop = -1
             e.HasMorePages = true;
             return;
          }
      }
    }
}
private void btnPrint_Click(object sender, EventArgs e) {
    i = 1;
    PaperSize paperSize = new PaperSize("MyCustomSize", 100, 65);
    paperSize.RawKind = (int)PaperKind.Custom;
    printDocument1.DefaultPageSettings.PaperSize = paperSize;
    printDocument1.Print();
}
于 2013-10-22T20:38:58.013 回答