3

I'm having trouble printing a receipt in an acceptable manner for a winforms application I'm working on, and I'm attaching my code, the receipt i'm printing, and another image of a receipt I want to print something like it

here is my code

 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;


        }


    }

now as you see, I have it printing the invoice number and the items that are pulled into the data grid view using a for statement

here is how this code is printing

enter image description here

now I want to create a table layout from the datagridview items to look like this receipt in the image below

enter image description here

Joey Arnanzo
  • 329
  • 3
  • 18
  • It is funny to see that Arabic food receipt here, though I am already hungry ;). So the main requisite is you want to change the design of the table that is being printed? – Mohammed Noureldin May 18 '18 at 03:52
  • first Lol, Mohammed :) its a grocery store but yeah... secondly, yeah.. I want to make it look almost exactly like this – Joey Arnanzo May 18 '18 at 03:55
  • I would use other English text receipts (as well the text in code), so the others are much more comfortable to help. I guess the may be a bit confused because of the Arabic text. However I am checking it and trying to figure out what the problem is. – Mohammed Noureldin May 18 '18 at 03:56
  • unfortunately I don't have an example receipt of what I'm trying to reach except arabic ones as I'm currently located in Egypt. – Joey Arnanzo May 18 '18 at 03:57
  • Ok I see now that your question is not specific, because your code does not have any problem, and it shows what it should show. What you need to get that design is a bit bold to the text, some rectangles in the position of the text. It is just about some calculations to give the correct x and y for the rectangles you have to create. What is the problem in doing this? – Mohammed Noureldin May 18 '18 at 04:05
  • I'm trying to do this but I can't, I'm trying to use the ----- to create the look but I think I'm messing it up. is there another way to acheive this?/ – Joey Arnanzo May 18 '18 at 04:14
  • Also, please take a look on the following link, the its code is more cleaner: https://stackoverflow.com/questions/13390313/pos-application-development-receipt-printing?rq=1 – Mohammed Noureldin May 18 '18 at 04:14
  • Why to use ----, why not drawing a rectangle with a small height? (I am not sure if there is a line). – Mohammed Noureldin May 18 '18 at 04:15
  • Ok, maybe just I'm not that good in English, but I don't know how to draw the rectangle, I searched and didn't really understand – Joey Arnanzo May 18 '18 at 04:16
  • What about this: `graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);`? it is written by you. You may add another line like this: `graphic.FillRectangle(white, 0, 100, bitm.Width, 5);` Does this work? – Mohammed Noureldin May 18 '18 at 04:18
  • Actually I took this from my Barcode printing code, this code is the rectangle drawing code? I feel stupid now... could you please tell me how to make border lines so I can use it? – Joey Arnanzo May 18 '18 at 04:20
  • Everyone needs to learn, there is no problem in asking, you just had to read the code more accurately. Yes it seems to be, however, take a look at the following as well, here is a good small snipped of code to draw a rectangle. https://stackoverflow.com/questions/925509/border-in-drawrectangle?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Mohammed Noureldin May 18 '18 at 04:22
  • Does that solve the problem? if yes I am going to add it as an answer. – Mohammed Noureldin May 18 '18 at 04:23
  • 1
    Thank you Mohammed, I will def. try this right now and see, will get back to you, but i think this is the way, thank you for pointing me to the right direction – Joey Arnanzo May 18 '18 at 04:28
  • no problem, hope that helps, I added an answer, if there is still anything unclear, I will edit the answer rather than commenting here. – Mohammed Noureldin May 18 '18 at 04:34
  • 1
    No I think it is all clear now, I've tried it and it works now, just need to fix the positioning of the text to make it aligned right when printed and all will be good. Thanks a lot my friend – Joey Arnanzo May 18 '18 at 04:35

1 Answers1

2

What you need is drawing some rectangles on the correct positions to get that design you see in the second receipt.

You already draw a white-filled rectangle in your code.

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

You just need to repeat that in the correct positions with the correct dimensions to achieve the desired design.

Here you can see another detailed approach which may help you to draw a rectangle:

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

Taken from: Border in DrawRectangle

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99