Thursday 8 May 2014

Windows Printing

 PrintDocument doc = new PrintDocument();
                doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
                PrintDialog dlgSettings = new PrintDialog();
                dlgSettings.Document = doc;
                doc.Print();

void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                // PictureBox abc = pictureBox1;
                Font font5 = new Font("Arial", 9);
                Font font4 = new Font("Arial", 8);
                Font font3 = new Font("Arial", 12);
                Font font2 = new Font("Arial", 9);
                Font font1 = new Font("Arial", 13);
                Font font = new Font("Arial", 10);
                Font font7 = new Font("Arial", 20);
                Font font8 = new Font("Times New Roman", 27);
                Font font6 = new Font("Arial", 17);
                Font font9 = new Font("Arial", 14);
                //float x = e.MarginBounds.Left;
                //float lineSide=font.
                float y = e.MarginBounds.Top;
                float lineHeight = font.GetHeight(e.Graphics);
                //e.Graphics.DrawImage(abc.Image, 60, 70, 50, 100);
                //e.Graphics.DrawImage(abc.Image, 650, 70, 50, 100);
                y += lineHeight;

                y += lineHeight;
                //e.Graphics.DrawImageUnscaled(adc, 0, 0, 768, 1152);
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("20"), float.Parse("12"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("20"), float.Parse("140"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("20"), float.Parse("260"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("20"), float.Parse("300"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("20"), float.Parse("837"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("20"), float.Parse("950"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("20"), float.Parse("1035"));
                y += lineHeight;


                int aa = 20;
                while (aa < 1040)
                {
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("20"), float.Parse(aa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("835"), float.Parse(aa.ToString()));
                    y += lineHeight;
                    aa = aa + 12;
                }
                int aaaa = 145;
                while (aaaa < 255)
                {
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("560"), float.Parse(aaaa.ToString()));
                    y += lineHeight;

                    aaaa = aaaa + 12;
                }

                e.Graphics.DrawString("BILL BOOK", font, Brushes.Black, float.Parse("380"), float.Parse("40"));
                e.Graphics.DrawString("Mobile : 9810067502", font, Brushes.Black, float.Parse("670"), float.Parse("40"));
                e.Graphics.DrawString("PAHUJA ASSOCIATES", font8, Brushes.Black, float.Parse("228"), float.Parse("60"));

                e.Graphics.DrawString("351/1/11, New Colony More, Ratan Garden", font3, Brushes.Black, float.Parse("253"), float.Parse("100"));
                e.Graphics.DrawString("Old Railway Road, Gurgaon", font3, Brushes.Black, float.Parse("310"), float.Parse("120"));


                e.Graphics.DrawString("Date   ", font, Brushes.Black, float.Parse("570"), float.Parse("220"));
                e.Graphics.DrawString(": " + dateTimePicker1.Text, font, Brushes.Black, float.Parse("670"), float.Parse("220"));
                e.Graphics.DrawString("TIN NO.  ", font, Brushes.Black, float.Parse("30"), float.Parse("125"));
                e.Graphics.DrawString(": 06401939735", font, Brushes.Black, float.Parse("105"), float.Parse("125"));

                e.Graphics.DrawString("Name ", font, Brushes.Black, float.Parse("30"), float.Parse("155"));
                e.Graphics.DrawString(": " + textBox8.Text, font, Brushes.Black, float.Parse("100"), float.Parse("155"));

                e.Graphics.DrawString("Address", font, Brushes.Black, float.Parse("30"), float.Parse("175"));
                e.Graphics.DrawString(": ", font, Brushes.Black, float.Parse("100"), float.Parse("175"));
                //Below Code for Multiline print of Address

                string ss1 = textBox9.Text;
                int count1 = textBox9.Text.Length;
                int divlength1 = 60;
                int remainder1 = count1 % divlength1;

                int NoOfRow1 = 0;
                if (remainder1 > 0)
                {
                    NoOfRow1 = (count1 / divlength1) + 1;
                }
                else
                {
                    NoOfRow1 = count1 / divlength1;
                }
                string[] arr1 = new string[NoOfRow1];
                for (int i = 0; i < NoOfRow1; i++)
                {
                    if (i < NoOfRow1 - 1)
                    {
                        arr1[i] = ss1.Substring(60 * i, 60);
                    }
                    else
                    {
                        arr1[i] = ss1.Substring(60 * i, remainder1);
                    }
                }
                int cd1 = 175;
                for (int i = 0; i < NoOfRow1; i++)
                {
                    e.Graphics.DrawString(arr1[i], font, Brushes.Black, float.Parse("110"), float.Parse(cd1.ToString()));
                    y += lineHeight;
                    cd1 = cd1 + 20;
                }

                //===============================================
                //Customer related
                e.Graphics.DrawString("Invoice No.  ", font, Brushes.Black, float.Parse("670"), float.Parse("125"));
                e.Graphics.DrawString(": " + label24.Text, font, Brushes.Black, float.Parse("740"), float.Parse("125"));
                e.Graphics.DrawString("Dispatch Date   ", font, Brushes.Black, float.Parse("570"), float.Parse("240"));
                e.Graphics.DrawString(": " + dateTimePicker2.Text, font, Brushes.Black, float.Parse("670"), float.Parse("240"));

                e.Graphics.DrawString("P.Order No.  ", font, Brushes.Black, float.Parse("570"), float.Parse("160"));
                e.Graphics.DrawString(": " + textBox23.Text, font, Brushes.Black, float.Parse("670"), float.Parse("160"));
                e.Graphics.DrawString("VAT/TIN No.  ", font, Brushes.Black, float.Parse("570"), float.Parse("180"));
                e.Graphics.DrawString(": " + textBox18.Text, font, Brushes.Black, float.Parse("670"), float.Parse("180"));
                e.Graphics.DrawString("C.S.T No  ", font, Brushes.Black, float.Parse("570"), float.Parse("200"));
                e.Graphics.DrawString(": " + textBox13.Text, font, Brushes.Black, float.Parse("670"), float.Parse("200"));

                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));
                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("100"), float.Parse("210"));

                int aaa = 265;
                while (aaa < 840)
                {
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("20"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("83"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("250"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("290"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("350"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("410"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("450"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("510"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("580"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("620"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("660"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("700"), float.Parse(aaa.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("740"), float.Parse(aaa.ToString()));
                    y += lineHeight;

                    aaa = aaa + 12;
                }
                //itemcode,itemname,noofcase,mfgperiod,mrp,qty,rate,amount,discper,vatper,surcper,netamount,pcscase
                e.Graphics.DrawString("Item Code", font5, Brushes.Black, float.Parse("25"), float.Parse("276"));

                e.Graphics.DrawString("Item Name", font5, Brushes.Black, float.Parse("105"), float.Parse("276"));

                e.Graphics.DrawString("No.Of", font5, Brushes.Black, float.Parse("255"), float.Parse("276"));
                e.Graphics.DrawString("Case", font5, Brushes.Black, float.Parse("255"), float.Parse("288"));
                e.Graphics.DrawString("Mfg.", font5, Brushes.Black, float.Parse("295"), float.Parse("276"));
                e.Graphics.DrawString("Period", font5, Brushes.Black, float.Parse("295"), float.Parse("288"));
                e.Graphics.DrawString("M.R.P", font5, Brushes.Black, float.Parse("355"), float.Parse("276"));
                e.Graphics.DrawString("Qty", font5, Brushes.Black, float.Parse("415"), float.Parse("276"));
                e.Graphics.DrawString("Rate", font5, Brushes.Black, float.Parse("455"), float.Parse("276"));
                e.Graphics.DrawString("Amount", font5, Brushes.Black, float.Parse("515"), float.Parse("276"));
                e.Graphics.DrawString("Dis.%", font5, Brushes.Black, float.Parse("585"), float.Parse("276"));

                e.Graphics.DrawString("Vat%", font5, Brushes.Black, float.Parse("625"), float.Parse("276"));

                e.Graphics.DrawString("Sur.%", font5, Brushes.Black, float.Parse("665"), float.Parse("276"));

                e.Graphics.DrawString("Pcs/", font5, Brushes.Black, float.Parse("705"), float.Parse("276"));
                e.Graphics.DrawString("Case", font5, Brushes.Black, float.Parse("705"), float.Parse("288"));
                e.Graphics.DrawString("Net Amount", font5, Brushes.Black, float.Parse("755"), float.Parse("276"));

                e.Graphics.DrawString("Amount", font, Brushes.Black, float.Parse("650"), float.Parse("850"));
                e.Graphics.DrawString("Total Vat", font, Brushes.Black, float.Parse("50"), float.Parse("880"));
                e.Graphics.DrawString("Total Surcharge", font, Brushes.Black, float.Parse("250"), float.Parse("880"));
                e.Graphics.DrawString("Total Discount", font, Brushes.Black, float.Parse("450"), float.Parse("880"));
                e.Graphics.DrawString("Total Amount", font, Brushes.Black, float.Parse("650"), float.Parse("880"));
                e.Graphics.DrawString("Scheme", font, Brushes.Black, float.Parse("50"), float.Parse("910"));
                e.Graphics.DrawString("H.C.", font, Brushes.Black, float.Parse("250"), float.Parse("910"));
                e.Graphics.DrawString("CR/DR", font, Brushes.Black, float.Parse("450"), float.Parse("910"));
                e.Graphics.DrawString("Net Amount", font, Brushes.Black, float.Parse("650"), float.Parse("910"));

                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("735"), float.Parse("850"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("120"), float.Parse("880"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("350"), float.Parse("880"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("540"), float.Parse("880"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("735"), float.Parse("880"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("120"), float.Parse("910"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("350"), float.Parse("910"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("540"), float.Parse("910"));
                e.Graphics.DrawString(" : ", font, Brushes.Black, float.Parse("735"), float.Parse("910"));

                e.Graphics.DrawString("" + textBox7.Text, font, Brushes.Black, float.Parse("770"), float.Parse("850"));
                e.Graphics.DrawString("" + textBox14.Text, font, Brushes.Black, float.Parse("140"), float.Parse("880"));
                e.Graphics.DrawString("" + textBox15.Text, font, Brushes.Black, float.Parse("370"), float.Parse("880"));
                e.Graphics.DrawString("" + textBox16.Text, font, Brushes.Black, float.Parse("560"), float.Parse("880"));
                e.Graphics.DrawString("" + textBox11.Text, font, Brushes.Black, float.Parse("770"), float.Parse("880"));
                e.Graphics.DrawString("" + textBox17.Text, font, Brushes.Black, float.Parse("140"), float.Parse("910"));
                e.Graphics.DrawString("" + textBox19.Text, font, Brushes.Black, float.Parse("370"), float.Parse("910"));
                e.Graphics.DrawString("" + textBox20.Text, font, Brushes.Black, float.Parse("560"), float.Parse("910"));
                e.Graphics.DrawString("" + textBox21.Text, font, Brushes.Black, float.Parse("770"), float.Parse("910"));
                e.Graphics.DrawString("E. & O.E.", font, Brushes.Black, float.Parse("30"), float.Parse("960"));
                e.Graphics.DrawString("Goods once sold will not be taken back.", font, Brushes.Black, float.Parse("30"), float.Parse("1000"));

                e.Graphics.DrawString("For PAHUJA ASSOCIATES", font, Brushes.Black, float.Parse("620"), float.Parse("960"));
                e.Graphics.DrawString("Authorised Signatory", font, Brushes.Black, float.Parse("650"), float.Parse("1020"));


                int c = 320;
                for (int b = 0; b < dataGridView1.Rows.Count - 1; b++)
                {
                    // int pos5 = position(counting(dataGridView1.Rows[b].Cells[1].Value.ToString()));
                    //e.Graphics.DrawString((b + 1).ToString(), font4, Brushes.Black, float.Parse("23"), float.Parse(c.ToString()));
                    //y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[0].Value.ToString(), font5, Brushes.Black, float.Parse("25"), float.Parse(c.ToString()));
                    y += lineHeight;
                    //e.Graphics.DrawString(dataGridView1.Rows[b].Cells[1].Value.ToString(), font5, Brushes.Black, float.Parse("86"), float.Parse(c.ToString()));
                    //y += lineHeight;


                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[2].Value.ToString(), font5, Brushes.Black, float.Parse("255"), float.Parse((c).ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(Convert.ToDateTime(dataGridView1.Rows[b].Cells[3].Value).ToString("dd-MM-yy"), font5, Brushes.Black, float.Parse("295"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[4].Value.ToString(), font5, Brushes.Black, float.Parse("355"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[5].Value.ToString(), font5, Brushes.Black, float.Parse("415"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[6].Value.ToString(), font5, Brushes.Black, float.Parse("455"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[7].Value.ToString(), font5, Brushes.Black, float.Parse("515"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[8].Value.ToString(), font5, Brushes.Black, float.Parse("585"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[10].Value.ToString(), font5, Brushes.Black, float.Parse("625"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[12].Value.ToString(), font5, Brushes.Black, float.Parse("665"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[15].Value.ToString(), font5, Brushes.Black, float.Parse("705"), float.Parse(c.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[14].Value.ToString(), font5, Brushes.Black, float.Parse("755"), float.Parse(c.ToString()));
                    y += lineHeight;

                    //Below Code for Multiline print of Item Name

                    string ss = dataGridView1.Rows[b].Cells[1].Value.ToString();
                    int count = dataGridView1.Rows[b].Cells[1].Value.ToString().Length;
                    int divlength = 23;
                    int remainder = count % divlength;

                    int NoOfRow = 0;
                    if (remainder > 0)
                    {
                        NoOfRow = (count / divlength) + 1;
                    }
                    else
                    {
                        NoOfRow = count / divlength;
                    }
                    string[] arr = new string[NoOfRow];
                    for (int i = 0; i < NoOfRow; i++)
                    {
                        if (i < NoOfRow - 1)
                        {
                            arr[i] = ss.Substring(23 * i, 23);
                        }
                        else
                        {
                            arr[i] = ss.Substring(23 * i, remainder);
                        }
                    }

                    for (int i = 0; i < NoOfRow; i++)
                    {
                        e.Graphics.DrawString(arr[i], font, Brushes.Black, float.Parse("86"), float.Parse(c.ToString()));
                        y += lineHeight;
                        c = c + 20;
                    }
                    //======================================================

                    //c = c + 15;
                }

            }
            catch (Exception)
            {
            }
        }

No comments:

Post a Comment