如何将asp.net的printeventargs对象传递给javascript
Posted
技术标签:
【中文标题】如何将asp.net的printeventargs对象传递给javascript【英文标题】:how to pass printeventargs object of asp.net to javascript 【发布时间】:2022-01-16 21:45:47 【问题描述】:我正在尝试优化代码并对旧的现有代码进行一些更改。我必须实现从服务器端到客户端的打印账单我尝试了太多方法但我没有找到任何一种这是我的代码
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 进行打印,这实际上让您无法控制 - 您可以调用 thewindow.print
method 来打印当前文档的内容,仅此而已。
不,我正在尝试将该 printpageventargs 对象传递给 javascript,然后我可以使用 javascript 进行打印。你知道 printpageeventargs 对象传递到 javascript window.write 函数作为参数的任何方法
您无法从 Javascript 控制打印!您所能做的就是拨打window.print()
;您无法控制打印机设置,也无法生成页面内容以外的内容。
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
【参考方案1】:
每当您在 javascript 中使用打印对象时,我都会得到答案 **它读起来像 html 页面 **window.print();
获取整个页面并打印,但是当您使用时
你可以看到我在下面的打印函数中写了一个文本,我们可以像 html 页面一样编写和格式化,或者我们可以创建一个 html 页面并传递然后打印该 html 页面 print
prtwin.document.write('hello wolrd sjks')
【讨论】:
以上是关于如何将asp.net的printeventargs对象传递给javascript的主要内容,如果未能解决你的问题,请参考以下文章
如何将 ASP.NET 网站转换为 ASP.NET Web 应用程序
选择 webApi 模板时如何将 ASP.Net 身份添加到 Asp.Net Core?
如何将 facebook 帐户与 asp.net 网站集成?
如何使用 asp.net 将 gridview 图像和数据导出到 PDF 或使用 asp.net 将 gridview 数据导出到 PDF