PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] how to pass printeventargs object of asp.net to javascript

 September 18, 2022     asp.net, javascript, printing, web     No comments   

Issue

I am trying to optimize the code and do some changes to the old existing code. I have to implement print bill from the server side to the client side I try too many ways but I did not find any of one this is my code

 public void Print()
        {
            var doc = new PrintDocument();
            string pkInstalledPrinters = string.Empty; +
            //var doc = new PrintDocument();
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count;)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                break;
                //comboInstalledPrinters.Items.Add(pkInstalledPrinters); 
            }
            // doc.PrinterSettings.PrinterName = "\\\\W95053\\P12562PCL";
            doc.PrinterSettings.PrinterName = pkInstalledPrinters;
            doc.PrinterSettings.Copies = 2;
            doc.PrintPage += new PrintPageEventHandler(ProvideContent);
            doc.Print();
            doc.Print();

this is server-side code for printing

       public void ProvideContent(object sender, PrintPageEventArgs e)
        {
            DateTime koDate = DateTime.Now.AddMinutes(-320);
            string totalAmount = TotalAmount.Value;
            //e.Graphics.DrawString("7 Plates Restaurant", new Font("Arial", 12), Brushes.Black, 50, 0);
            //e.Graphics.DrawString("Item", new Font("Arial", 11), Brushes.Black, 15, 30);
            //e.Graphics.DrawString("Qty", new Font("Arial", 11), Brushes.Black, 150, 30);
            //e.Graphics.DrawString("Rate", new Font("Arial", 11), Brushes.Black, 185, 30);
            string addressLine1 = hotel.Address;
            string addressLine2 = "Bangalore 560046";
            string phoneNo = "PH – " + hotel.PhoneNo;
            string date = koDate.Date.ToShortDateString();
            string tin = "29061276984";
            string time = koDate.ToShortTimeString();
            string cashier = "Sameer";
            e.Graphics.DrawString("7 Plates", new Font("Cambria", 24, FontStyle.Bold), Brushes.Black, 60, 0);
            e.Graphics.DrawString("Restaurant", new Font("Cambria", 11), Brushes.Black, 90, 40);
            e.Graphics.DrawString(addressLine1, new Font("Cambria", 11), Brushes.Black, 10, 60);
            e.Graphics.DrawString(addressLine2, new Font("Cambria", 11), Brushes.Black, 65, 75);
            e.Graphics.DrawString(phoneNo, new Font("Cambria", 11), Brushes.Black, 20, 90);
            // e.Graphics.DrawString(webSite, new Font("Cambria", 11), Brushes.Black, 70, 90);
            e.Graphics.DrawString("Order No: " + orderNumber, new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 0, 110);
            e.Graphics.DrawString("Bill No: " + billNumber, new Font("Cambria", 11), Brushes.Black, 170, 110);
            e.Graphics.DrawString("Date: " + date, new Font("Cambria", 11), Brushes.Black, 0, 125);
            e.Graphics.DrawString("Time: " + time, new Font("Cambria", 11), Brushes.Black, 170, 125);
            e.Graphics.DrawString("Tin: " + tin, new Font("Cambria", 11), Brushes.Black, 0, 140);
            e.Graphics.DrawString("No.", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 0, 160);
            e.Graphics.DrawString("Item", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 25, 160);
            e.Graphics.DrawString("Qty", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 140, 160);
            e.Graphics.DrawString("Rate", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 175, 160);
            e.Graphics.DrawString("Total", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 220, 160);

            int xAxis = 0;
            int yAxis = 180;
            int itemNo = 0;
            int totalValue = 0;
            foreach (DayEntryHR item in dayEntryList)
            {
                itemNo++;
                string no = Convert.ToString(itemNo) + ".";
                string itemName = item.MasterItem.Item;
                int rateMaster = item.MasterItem.KORate;
                double rate1 = Convert.ToInt32(rateMaster / 1.12) + 1;
                string rate = Convert.ToString(rate1);
                string total = Convert.ToString(rate1 * item.Quantity);
                string quantity = Convert.ToString(item.Quantity);
                totalValue = totalValue + Convert.ToInt32(total);
                //e.Graphics.DrawString(
                //  "Hello world",
                //  new Font("Arial", 12),
                //  Brushes.Black,
                //  e.MarginBounds.Left,
                //  e.MarginBounds.Top);

                e.Graphics.DrawString(no, new Font("Cambria", 11), Brushes.Black, xAxis, yAxis);
                xAxis = xAxis + 25;
                e.Graphics.DrawString(itemName, new Font("Cambria", 11), Brushes.Black, xAxis, yAxis);
                xAxis = xAxis + 120;
                e.Graphics.DrawString(quantity, new Font("Cambria", 11), Brushes.Black, xAxis, yAxis);
                xAxis = xAxis + 30;
                e.Graphics.DrawString(rate, new Font("Cambria", 11), Brushes.Black, xAxis, yAxis);
                xAxis = xAxis + 50;
                e.Graphics.DrawString(total, new Font("Cambria", 11), Brushes.Black, xAxis, yAxis);
                xAxis = 0;
                yAxis = yAxis + 15;
                //e.Graphics.DrawString("2.", new Font("Cambria", 11), Brushes.Black, 0, 195);
                //e.Graphics.DrawString(c, new Font("Cambria", 11), Brushes.Black, 25, 195);
                //e.Graphics.DrawString("2", new Font("Cambria", 11), Brushes.Black, 145, 195);
                //e.Graphics.DrawString("150", new Font("Cambria", 11), Brushes.Black, 175, 195);
                //e.Graphics.DrawString("150", new Font("Cambria", 11), Brushes.Black, 225, 195);
                //e.Graphics.DrawString("3.", new Font("Cambria", 11), Brushes.Black, 0, 210);
                //e.Graphics.DrawString(d, new Font("Cambria", 11), Brushes.Black, 25, 210);
                //e.Graphics.DrawString("5", new Font("Cambria", 11), Brushes.Black, 145, 210);
                //e.Graphics.DrawString("150", new Font("Cambria", 11), Brushes.Black, 175, 210);
                //e.Graphics.DrawString("300", new Font("Cambria", 11), Brushes.Black, 225, 210);
                //e.Graphics.DrawString("4.", new Font("Cambria", 11), Brushes.Black, 0, 225);
                //e.Graphics.DrawString(f, new Font("Cambria", 11), Brushes.Black, 25, 225);
                //e.Graphics.DrawString("2", new Font("Cambria", 11), Brushes.Black, 145, 225);
                //e.Graphics.DrawString("110", new Font("Cambria", 11), Brushes.Black, 175, 225);
                //e.Graphics.DrawString("220", new Font("Cambria", 11), Brushes.Black, 225, 225);
            }

            int vat = ((totalValue * 12) / 100);
            //e.Graphics.DrawString("Vat 5.5%:", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 140, yAxis + 5);
            //e.Graphics.DrawString(Convert.ToString(Vat), new Font("Cambria", 11), Brushes.Black, 220, yAxis + 5);
            e.Graphics.DrawString("Total:", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 140, yAxis + 5);
            e.Graphics.DrawString(Convert.ToString(totalValue), new Font("Cambria", 11), Brushes.Black, 220, yAxis + 5);
            e.Graphics.DrawString("Pay By: Cash", new Font("Cambria", 11, FontStyle.Bold), Brushes.Black, 140, yAxis + 23);
            e.Graphics.DrawString("To provide feedback call us", new Font("Cambria", 11), Brushes.Black, 35, yAxis + 40);
            e.Graphics.DrawString("Or", new Font("Cambria", 11), Brushes.Black, 100, yAxis + 55);
            e.Graphics.DrawString("Visit us on www.7plates.in", new Font("Cambria", 11), Brushes.Black, 37, yAxis + 70);
            e.Graphics.DrawString("Thank You", new Font("Cambria", 14, FontStyle.Bold), Brushes.Black, 75, yAxis + 90);

            string name = Customer.Value + " " + LastName.Value;
            bool flag = false;
            e.Graphics.DrawString(name, new Font("Cambria", 14), Brushes.Black, 75, yAxis + 110);
            if (Homedelevery.Checked == true)
            {

                string addressline1 = string.Empty;
                string addressline2 = string.Empty;

                string address = Address.Value;
                if (address.Length > 28)
                {
                    addressline1 = address.Substring(0, 28);
                    addressline2 = address.Substring(28);
                    flag = true;
                }
                e.Graphics.DrawString("Address:", new Font("Cambria", 11), Brushes.Black, 0, yAxis + 140);
                if (flag == true)
                {
                    e.Graphics.DrawString(addressline1, new Font("Cambria", 11), Brushes.Black, 63, yAxis + 140);
                    e.Graphics.DrawString(addressline2, new Font("Cambria", 11), Brushes.Black, 63, yAxis + 160);
                    e.Graphics.DrawString("LandMark:", new Font("Cambria", 11), Brushes.Black, 0, yAxis + 180);
                    e.Graphics.DrawString(Landmark.Value, new Font("Cambria", 11), Brushes.Black, 70, yAxis + 180);
                }
                else
                {
                    e.Graphics.DrawString(address, new Font("Cambria", 11), Brushes.Black, 63, yAxis + 140);
                    e.Graphics.DrawString("LandMark:", new Font("Cambria", 11), Brushes.Black, 0, yAxis + 160);
                    e.Graphics.DrawString(Landmark.Value, new Font("Cambria", 11), Brushes.Black, 73, yAxis + 160);
                }
            }
            if (Takeaway.Checked == true)
            {
                e.Graphics.DrawString("Phone:", new Font("Cambria", 11), Brushes.Black, 0, yAxis + 140);
                e.Graphics.DrawString(Phone1.Value, new Font("Cambria", 11), Brushes.Black, 50, yAxis + 140);
                e.Graphics.DrawString("TakeAway", new Font("Cambria", 14, FontStyle.Bold), Brushes.Black, 73, yAxis + 160);
            }
            else if (Homedelevery.Checked == true)
            {
                if (flag == true)
                {
                    e.Graphics.DrawString("Phone:", new Font("Cambria", 11), Brushes.Black, 0, yAxis + 200);
                    e.Graphics.DrawString(Phone1.Value, new Font("Cambria", 11), Brushes.Black, 50, yAxis + 200);
                    e.Graphics.DrawString("Home Delevery", new Font("Cambria", 14, FontStyle.Bold), Brushes.Black, 70, yAxis + 220);
                }
                else
                {
                    e.Graphics.DrawString("Phone:", new Font("Cambria", 11), Brushes.Black, 0, yAxis + 180);
                    e.Graphics.DrawString(Phone1.Value, new Font("Cambria", 11), Brushes.Black, 50, yAxis + 180);
                    e.Graphics.DrawString("Home Delevery", new Font("Cambria", 14, FontStyle.Bold), Brushes.Black, 70, yAxis + 200);
                }
            }

            Pen myPen = new Pen(System.Drawing.Color.Black, 1);
            e.Graphics.DrawLine(myPen, 0, 176, 260, 176);
            e.Graphics.DrawLine(myPen, 0, yAxis + 5, 260, yAxis + 5);
            e.Graphics.DrawLine(myPen, 0, yAxis + 21, 260, yAxis + 21);

            //e.Graphics.DrawString(no, new Font("Arial", 10), Brushes.Black, xAxis, yAxis);
            //xAxis = xAxis + 15;
            //e.Graphics.DrawString(itemName, new Font("Arial", 10), Brushes.Black, xAxis, yAxis);
            //xAxis = xAxis + 145;
            //e.Graphics.DrawString(quantity, new Font("Arial", 10), Brushes.Black, xAxis, yAxis);
            //xAxis = xAxis + 30;
            //e.Graphics.DrawString(rate, new Font("Arial", 10), Brushes.Black, xAxis, yAxis);
            //yAxis = yAxis + 20;
            //xAxis = 0;

        }

I search a lot of stuff and I am trying to do this but I could not pass the object that I want to print

 protected void Submit(object sender, EventArgs e)
        {

            var doc = new PrintDocument();
            doc.PrinterSettings.Copies = 2;
            doc.PrintPage += new PrintPageEventHandler(ProvideContent);

            //String msg = "alert('" + doc + "button)'";
            ClientScript.RegisterStartupScript(this.GetType(), "alert", string.Format(@"
                                                                                           alert('u r inside a fucntion');
                                                    var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=100,tollbar=0,scrollbars=1,status=0,resizable=1');
                                                    prtwin.document.write('hello wolrd sjks');
                                                    prtwin.document.close();
                                                    prtwin.focus();
                                                    prtwin.print();
                                                    prtwin.close(); "
                                                , new PrintPageEventHandler(ProvideContent)), true);
            //ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('heelo world');", true);
        }

thanks to read this long question


Solution

I got the answer whenever u use a print object in javascript **it read like HTML page **window.print(); take the whole page and print but when u

u can see i write a text in below print function we can write and format like html page or we can create an html page and pass and then print that html page print prtwin.document.write('hello wolrd sjks')



Answered By - Noman
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing