如何在c#中给出面板的确切尺寸?

时间:2014-08-30 06:08:02

标签: c# printing size

我不是英文用户,请原谅我的语法问题。微笑| :) 我有一些纸质表格,我想在它们上面打印一些标签。这些纸张有特殊的尺寸(24厘米* 14)。所以我制作了一个面板(907像素* 529像素),我把标签放在上面(我换了厘米)像素和我把标签放在我的面板的特殊部分)。这些标签将打印在我的纸张表格的空白区域。但问题是,只有第一个表格可以打印正确的样式。其他人是印在表格的上方。我想这可能是因为我没有给我的面板和标签确切的像素大小。但我不能给我的面板确切的大小,以像素为单位,因为它不接受十进制像素。有任何想法吗? 这是我的代码的一部分:

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Pen blackPen = new Pen(Color.Black, 1);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.DrawLine(blackPen, 188, 400, 302, 400);
            e.Graphics.DrawRectangle(blackPen, 330, 319, 122, 55);
            e.Graphics.DrawString(label20.Text, label20.Font, new SolidBrush(label20.ForeColor), label20.Location);
            e.Graphics.DrawString(label21.Text, label21.Font, new SolidBrush(label21.ForeColor), label21.Location);
            e.Graphics.DrawString(label23.Text, label23.Font, new SolidBrush(label23.ForeColor), label23.Location);
            e.Graphics.DrawString(label24.Text, label24.Font, new SolidBrush(label24.ForeColor), label24.Location);
            e.Graphics.DrawString(label25.Text, label25.Font, new SolidBrush(label25.ForeColor), label25.Location);
            e.Graphics.DrawString(label26.Text, label26.Font, new SolidBrush(label26.ForeColor), label26.Location);
            e.Graphics.DrawString(label27.Text, label27.Font, new SolidBrush(label27.ForeColor), label27.Location);
e.Graphics.DrawString(lbl_kod.Text, lbl_kod.Font, new SolidBrush(lbl_kod.ForeColor), lbl_kod.Location);
            e.Graphics.DrawString(lbl_ttarikh.Text, lbl_ttarikh.Font, new SolidBrush(lbl_ttarikh.ForeColor), lbl_ttarikh.Location);
            e.Graphics.DrawString(lbl_ctot25.Text, lbl_ctot25.Font, new SolidBrush(lbl_ctot25.ForeColor), lbl_ctot25.Location);
            e.Graphics.DrawString(lbl_pricetot25.Text, lbl_pricetot25.Font, new SolidBrush(lbl_pricetot25.ForeColor), lbl_pricetot25.Location);
            e.Graphics.DrawString(lbl_pricetot.Text, lbl_pricetot.Font, new SolidBrush(lbl_pricetot.ForeColor), lbl_pricetot.Location);
            e.Graphics.DrawString(lbl_pricetoth.Text, lbl_pricetoth.Font, new SolidBrush(lbl_pricetoth.ForeColor), lbl_pricetoth.Location);
            e.Graphics.DrawString(lbl_name.Text, lbl_name.Font, new SolidBrush(lbl_name.ForeColor), lbl_name.Location);
            e.Graphics.DrawString(lbl_dtarikh.Text, lbl_dtarikh.Font, new SolidBrush(lbl_dtarikh.ForeColor), lbl_dtarikh.Location);        

            }

以及每种纸张的打印区域:

for (int i = 0; i < dataGridView1.RowCount && k < 3878; i++)
            {
                k = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
                bool found = false;
                ctot25 = Convert.ToInt32(dataGridView1.Rows[i].Cells[13].Value);
                ctot50 = Convert.ToInt32(dataGridView1.Rows[i].Cells[14].Value);
                ctot100 = Convert.ToInt32(dataGridView1.Rows[i].Cells[15].Value);
                foreach (object row1 in hoome)
                    if (row1.ToString() == (dataGridView1.Rows[i].Cells[0].Value).ToString())
                    {
                        found = true;
                    }

                if (found == false)
                {
                    if (i > 0 && ctot25 != 0 || ctot50 != 0 || ctot100 != 0)
                    {
                        #region tarikh
                        string date = dataGridView1.Rows[i].Cells[8].Value.ToString();
                        var aStringBuilder = new StringBuilder(date);
                        aStringBuilder.Insert(2, "/");
                        aStringBuilder.Insert(5, "/");
                        lbl_dtarikh.Text = aStringBuilder.ToString();

                        lbl_ttarikh.Text = datestringbuilder.ToString();
                        #endregion

                        decimal pricetot25;
                        pricetot25 = ctot25 * price25;
                        lbl_name.Text = (dataGridView1.Rows[i].Cells[1].Value).ToString();
                        lbl_kod.Text = (dataGridView1.Rows[i].Cells[0].Value).ToString();
                        lbl_ctot25.Text = (ctot25).ToString();
                        lbl_pricetot25.Text = (pricetot25).ToString();
                        lbl_pricetoth.Text = num2str(pricetot.ToString()) + " ریال" + "//";

                        #region print
                        System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
                        doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
                        PrintDialog PrintSettings = new PrintDialog();
                        PrintSettings.Document = doc;
                        PageSettings pgsetting = new PageSettings();
                        doc.Print();
                        #endregion
                    }
                }
            }

这是我的屏幕截图的链接: my panel

1 个答案:

答案 0 :(得分:0)

由于您的问题有点不清楚,我认为您最好不要在设计版面中添加面板并以编程方式创建它。如果你截取你的第二页的屏幕截图会更好。

修改

尝试在FOR循环中创建标签。您可以通过这种方式删除面板。我希望它有效

 if (found == false)
                {
                    if (i > 0 && ctot25 != 0 || ctot50 != 0 || ctot100 != 0)
                    {
                        //Somthethin

                        Label lbl_Name = new Label { Location = new Point(50, 50), Text = (dataGridView1.Rows[i].Cells[1].Value).ToString() };
                         this.Controls.Add(lbl_Name);

                         //The rest of the codes

                    }
                }