如何将 asp.net 的 printeventargs 对象传递给 javascript

how to pass printeventargs object of asp.net to javascript

我正在尝试优化代码并对现有的旧代码进行一些更改。我必须实现从服务器端到客户端的打印账单我尝试了太多方法但我没有找到任何一种这是我的代码

 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();

这是用于打印的服务器端代码

       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;

        }

我搜索了很多东西,我正在尝试这样做,但我无法传递我想要打印的对象

 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);
        }

感谢阅读这么长的问题

每当你在 javascript 中使用打印对象时我都会得到答案 **它看起来像 HTML 页 **window.print(); 打印整页但是当你

你可以看到我在下面的打印功能中写了一个文本,我们可以像 html 页面一样编写和格式化,或者我们可以创建一个 html 页面并传递然后打印 html页打印 prtwin.document.write('hello wolrd sjks')