c# - 在表格布局中打印收据 c# winforms 项目

标签 c# printing thermal-printer receipt

我无法以可接受的方式打印我正在处理的 winforms 应用程序的收据,我附上了我的代码、我正在打印的收据和我想打印的另一张收据图像类似的东西

这是我的代码

 private void printready()
        {
            string welcome = "Thank You For Visiting Dulabk";
            string InvoiceNo = txtInvoiceNo.Text;
            decimal gross = Convert.ToInt32(txtGross.Text);
            decimal net = Convert.ToInt32(txtNet.Text);
            decimal discount = gross - net;
            string InvoiceDate = dateTimePicker1.Value.ToLongDateString();

            int lineHeight = 20;
            int supplementaryLines = 15;

            Bitmap bitm = new Bitmap(welcome.Length * 30, (supplementaryLines + dataGridView1.Rows.Count) * lineHeight);
            StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
            using (Graphics graphic = Graphics.FromImage(bitm))
            {
                int startX = 0;
                int startY = 0;
                int offsetY = 0;
                Font newfont2 = null;
                Font itemFont = null;
                SolidBrush black = null;
                SolidBrush white = null;

                try
                {
                    //Font newfont = new Font("Arial Black", 8);
                    newfont2 = new Font("Calibri", 11);
                    itemFont = new Font("Calibri", 11);

                    black = new SolidBrush(Color.Black);
                    white = new SolidBrush(Color.White);

                    //PointF point = new PointF(40f, 2f);

                    graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                    graphic.DrawString("" + InvoiceNo + "رقم الفاتورة ", newfont2, black, startX + 150, startY + offsetY);
                    offsetY = offsetY + lineHeight;

                    //PointF pointPrice = new PointF(15f, 45f);
                    graphic.DrawString("" + InvoiceDate + "", newfont2, black, startX, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;

                    graphic.DrawString("إسم المنتج             " + "الكمية      " + "السعر", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("--------------------------------------------------", newfont2, black, startX, startY + offsetY);
                    //PointF pointPname = new PointF(10f, 65f);
                    //PointF pointBar = new PointF(10f, 65f);

                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;

                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        int ii = 1;
                        ii++;

                        graphic.DrawString(" " + dataGridView1.Rows[i].Cells[3].Value + "  " + dataGridView1.Rows[i].Cells[2].Value + "  " + dataGridView1.Rows[i].Cells[1].Value + "", itemFont,
                                 black, startX + 15, startY + offsetY);
                        offsetY = offsetY + lineHeight;
                    }
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("--------------------------------------------------", newfont2, black, startX, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("الإجمالي :" + gross + "", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("الخصم :" + discount + "", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("الصافي :" + net + "", newfont2, black, startX + 15, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                    offsetY = offsetY + lineHeight;
                    graphic.DrawString("--------------------------------------------------", newfont2, black, startX, startY + offsetY);
                    offsetY = offsetY + lineHeight;
                graphic.DrawString("" + welcome + "", newfont2, black, startX, startY + offsetY);
                offsetY = offsetY + lineHeight;
            }
            finally
            {
                black.Dispose();
                white.Dispose();
                itemFont.Dispose();
                newfont2.Dispose();
            }
        }

        using (MemoryStream Mmst = new MemoryStream())
        {
            bitm.Save("ms", ImageFormat.Jpeg);
            pictureBox1.Image = bitm;
            pictureBox1.Width = bitm.Width;
            pictureBox1.Height = bitm.Height;


        }


    }

现在如您所见,我使用 for 语句打印发票编号和拉入数据 GridView 的项目

这是这段代码的打印方式

enter image description here

现在我想从 datagridview 项目创建一个表格布局,看起来像下图中的这张收据

enter image description here

最佳答案

您需要在正确的位置绘制一些矩形,以获得您在第二张收据中看到的设计。

您已经在代码中绘制了一个白色填充的矩形。

graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);

您只需在正确的位置以正确的尺寸重复该操作即可实现所需的设计。

在这里您可以看到另一种详细的方法,可以帮助您绘制矩形:

Pen pen = new Pen(Color.Black, 2);
pen.Alignment = PenAlignment.Inset; //<-- change this to what ever you need
g.DrawRectangle(pen, rect);

取自:Border in DrawRectangle

关于c# - 在表格布局中打印收据 c# winforms 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50403468/

相关文章:

c# - 为什么 postsharp 导致 web 项目发布失败?

python print命令导致I/O错误

javascript - 从网站上使用 DYMO 标签打印机打印

c++ - 在 Linux ARM 上使用 libusb 的状态热敏打印机

c# - 如何将窗口所有者设置为非托管窗口

c# - 为什么这个属性会导致 StackOverFlow 异常?

c# - 在控制台应用程序的主线程上调用委托(delegate)

java - java中的PDF打印

java - Epson epos sdk 收据对齐问题

iOS蓝牙热敏票据打印机,支持哪款?