-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 に答える