在C#中的for循环内打印

时间:2013-10-22 19:54:56

标签: c# .net winforms printing

我正在尝试在for循环中打印,其中必须打印一个groupbox,直到条件满足。

//代码:

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

                }
            }
        }

FormImage:

Form which has a groupbox only this to be printed

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

我哪里错了?

2 个答案:

答案 0 :(得分: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();
    }
}

使用这样的简单形式:

Form

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

Printout

更新的代码

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

// 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));

仅抓取用于绘图的组框。

UpdatedScreen

答案 1 :(得分: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();
}
相关问题