【问题标题】:Print inside a for loop in C#在 C# 中的 for 循环内打印
【发布时间】:2013-10-22 19:54:55
【问题描述】:

我正在尝试在 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);

                }
            }
        }

表单图像:

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

我哪里错了?

【问题讨论】:

  • 迭代前Convert.ToInt32(lblTotalBox.Text返回什么?
  • 这将是不变的。可以是 5-10 之间的任何数字。
  • 什么是 printDocument1.Print()?
  • 你能在一个可能的异常上将语句包装在 try catch 和断点中吗?也许追踪.Print的电话?我的想法是 .Print 抛出一个没有发生的异常并没有使其进入 main(),因此应用程序不会崩溃。 . .(这是你的疯狂猜想!)
  • 您不需要using(Graphics g = e.Graphics) { },因为您不是创建新的图形对象,而是分配对现有图形对象的引用。此外,您不要在代码中使用g

标签: c# .net winforms printing


【解决方案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();
    }
}

用这样一个简单的形式:

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

更新代码

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

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

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

【讨论】:

  • 我正在打印整个表格。我只需要分组框。
  • 请在帖子中附上您的表格图片,以便我们知道您在说什么。无论如何更改var size=this.Size; 以更改位图的大小,并调整this.DrawToBitmap() 命令以适合您想要的UI 元素。
  • 添加了表单图像。请更新代码以仅打印组框。
  • 整个东西是一个组框,还是您对其他框之一感兴趣。从帖子中仍然不清楚是什么。
  • 所有控件都在组框内,我正在尝试打印整个组框。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    相关资源
    最近更新 更多