Friday 21 March 2014

How to Display without using Crystal Report(Windows Print)



  1. Select a Print Document, Print Preview Dialog, Print Dialog(all selected) under Printing Tools in   Toolbox.
  2. In Print Button
               //Create a PrintDocument object
                 PrintDocument pd = new PrintDocument();
                ////Add print-page event handler
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                //Set Document property of PrintPreviewDialog
                
                printPreviewDialog1.Document = pd;

                ((Form)printPreviewDialog1).WindowState = FormWindowState.Maximized;

                DialogResult dr = printPreviewDialog1.ShowDialog();

    3. public void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            SqlConnection con = new SqlConnection(conn);
            con.Open();

            Font font = new Font("Microsoft Sans Serif", 14F,
                                System.Drawing.FontStyle.Bold,
                                System.Drawing.GraphicsUnit.Point,
                                ((byte)(0)));

            Font font2 = new Font("Microsoft Sans Serif", 14F,
                          System.Drawing.FontStyle.Regular,
                          System.Drawing.GraphicsUnit.Point,
                             ((byte)(0)));
            Graphics g = e.Graphics;

            string vouno = txtCDNo.Text.ToString().Trim();
            string transdate = dtpDate.Value.ToString().Trim();
            transdate = transdate.Substring(0, 10);
            string recname = txtCSName.Text.ToString().Trim();
            string amount = txtAmount.Text.ToString().Trim();
            double amt = 0;
            if (amount != "")
            {
                amt = Convert.ToDouble(amount);
            }
            string narr1 = txtNarration.Text.ToString().Trim();
            string narr2 = txtNarration2.Text.ToString().Trim();

            string custoname = "";
            string addres1 = "";
            string addres2 = "";
            string addres3 = "";
            string city = "";
            string pincode = "";  

            if (radioCredit.Checked == true)
            {
                g.DrawString("CREDIT NOTE", font, System.Drawing.Brushes.Black, 50, 25);

                string find = "select * from IBS_CUSTOMER_MST where Cust_Id = '" + cid + "'";
                SqlCommand findquery = new SqlCommand(find, con);
                SqlDataReader dr = findquery.ExecuteReader();
                if (dr.Read())
                {
                    custoname = dr["Cust_Name"].ToString();
                    addres1 = dr["Cust_Add1"].ToString();
                    addres2 = dr["Cust_Add2"].ToString();
                    addres3 = dr["Cust_Add3"].ToString();
                    city = dr["Cust_City"].ToString();
                    pincode = dr["Cust_Pin"].ToString();
                }
                dr.Close();
            }
            else
            {
                g.DrawString("DEBIT NOTE", font, System.Drawing.Brushes.Black, 50, 25);

                string find = "select * from IBS_SUPPLIER_MST where Sup_Id = '" + cid + "'";
                SqlCommand findquery = new SqlCommand(find, con);
                SqlDataReader dr = findquery.ExecuteReader();
                if (dr.Read())
                {
                    custoname = dr["Sup_Name"].ToString();
                    addres1 = dr["Sup_Add1"].ToString();
                    addres2 = dr["Sup_Add2"].ToString();
                    addres3 = dr["Sup_Add3"].ToString();
                    city = dr["Sup_City"].ToString();
                    pincode = dr["Sup_Pin"].ToString();
                }
                dr.Close();
            }

            g.DrawString("Ledger Name", font, System.Drawing.Brushes.Black, 0, 75);
            g.DrawString("Address", font, System.Drawing.Brushes.Black, 0, 125);

            g.DrawString("Voucher No", font, System.Drawing.Brushes.Black, 450, 75);
            g.DrawString("Date", font, System.Drawing.Brushes.Black, 450, 125);

            g.DrawString("S.No", font, System.Drawing.Brushes.Black, 30, 230);
            g.DrawString("Descripation", font, System.Drawing.Brushes.Black, 90, 230);
            g.DrawString("Amount", font, System.Drawing.Brushes.Black, 400, 230);
            //g.DrawString("Address", font, System.Drawing.Brushes.Black, 300, 300);        

            int place = 150;

            g.DrawString(custoname, font2, System.Drawing.Brushes.Black, place, 75);
            g.DrawString(addres1, font2, System.Drawing.Brushes.Black, place, 125);
            g.DrawString(addres2, font2, System.Drawing.Brushes.Black, place, 145);
            g.DrawString(addres3, font2, System.Drawing.Brushes.Black, place, 165);
            g.DrawString(city, font2, System.Drawing.Brushes.Black, place, 185);
            g.DrawString(pincode, font2, System.Drawing.Brushes.Black, place, 205);

            g.DrawString(vouno, font2, System.Drawing.Brushes.Black, place + 450, 75);
            g.DrawString(transdate, font2, System.Drawing.Brushes.Black, place + 450, 125);

            g.DrawString(vouno, font2, System.Drawing.Brushes.Black, 30, 270);
            g.DrawString(narr1, font2, System.Drawing.Brushes.Black, 90, 270);
            g.DrawString(narr2, font2, System.Drawing.Brushes.Black, 90, 290);
            g.DrawString(amt.ToString("#0.00"), font2, System.Drawing.Brushes.Black, 400, 270);


            e.HasMorePages = false;  
        }

  4. Output Page