如何使用C#在ASP.NET中静默打印pdf?
问题描述:
我尝试使用C#在asp.net中静默打印PDF时遇到异常。有时我在打开PDF时出错。该进程找不到指定的文件。另外我也不希望pdf打开。
代码如下:
I am getting the exception while trying to print a PDF silently in asp.net with C#. At times I am getting error opening the PDF. the process cannot find the file specified. Also I don't want the pdf to open up either.
Code is as below:
private void print()
{
ProcessStartInfo infoPrintPdf = new ProcessStartInfo();
string pathPdf = @"E:\eSAFE\FileStore\PDF\Account Statement_76876876878_040111_000018473018_20160620172958468.pdf";
infoPrintPdf.FileName = pathPdf;
// The printer name is hardcoded here, but normally I get this from a combobox with all printers
string printerName = @"\\10.15.0.8\NTP Printer 001 SecondFloor";
//string printerName = "HP LaserJet Professional P1606dn";
string driverName = "hp1100sd.dll";
string portName = "10.15.0.23";
infoPrintPdf.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
infoPrintPdf.Arguments = string.Format("/t {0} \"{1}\" \"{2}\" \"{3}\"",
pathPdf, printerName, driverName, portName);
infoPrintPdf.CreateNoWindow = true;
infoPrintPdf.UseShellExecute = false;
infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden;
Process printPdf = new Process();
printPdf.StartInfo = infoPrintPdf;
printPdf.Start();
// This time depends on the printer, but has to be long enough to
// let the printer start printing
System.Threading.Thread.Sleep(10000);
if (!printPdf.CloseMainWindow()) // CloseMainWindow never seems to succeed
printPdf.Kill(); printPdf.WaitForExit(); // Kill AcroRd32.exe
printPdf.Close(); // Close the process and release resources
}
我的尝试:
代码如下:
What I have tried:
Code is as below:
private void print()
{
ProcessStartInfo infoPrintPdf = new ProcessStartInfo();
string pathPdf = @"E:\eSAFE\FileStore\PDF\Account Statement_76876876878_040111_000018473018_20160620172958468.pdf";
infoPrintPdf.FileName = pathPdf;
// The printer name is hardcoded here, but normally I get this from a combobox with all printers
string printerName = @"\\10.15.0.8\NTP Printer 001 SecondFloor";
//string printerName = "HP LaserJet Professional P1606dn";
string driverName = "hp1100sd.dll";
string portName = "10.15.0.23";
infoPrintPdf.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
infoPrintPdf.Arguments = string.Format("/t {0} \"{1}\" \"{2}\" \"{3}\"",
pathPdf, printerName, driverName, portName);
infoPrintPdf.CreateNoWindow = true;
infoPrintPdf.UseShellExecute = false;
infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden;
Process printPdf = new Process();
printPdf.StartInfo = infoPrintPdf;
printPdf.Start();
// This time depends on the printer, but has to be long enough to
// let the printer start printing
System.Threading.Thread.Sleep(10000);
if (!printPdf.CloseMainWindow()) // CloseMainWindow never seems to succeed
printPdf.Kill(); printPdf.WaitForExit(); // Kill AcroRd32.exe
printPdf.Close(); // Close the process and release resources
}
答
由于非常明显的原因,您无法在Web应用程序中以静默方式执行任何操作。想象一下你会发现一些悄悄使用客户端打印机的技巧,首先,这意味着在未经用户同意的情况下使用打印机(可能存在或不存在);然后想象用户弄清楚它为什么会发生。我怀疑用户会再次使用你的网站。
预留Web,即使在桌面应用程序中,也不应该静默打印:1)打印机属于用户,不是到你的申请; 2)印刷应该是罕见的事件;随着计算为社会铺平道路,人们打印得更少,节省纸张和碳粉;故意强迫用户打印是一种犯罪。
此外,PDF不是网络内容;它不是W3标准的一部分。不要被浏览器经常将PDF显示为网页这一事实所困惑;这都是非标准功能。因此,对于PDF,最好的策略是在通常的HTML锚点中提供简单的PDF文件。用户更了解如何处理它。打印该资源是PDF软件的工作,您没有和不能控制它;只有用户管理这些东西。-SA
You cannot do anything silently in a Web application, by pretty obvious reasons. Imagine for a second that you find some trick to use client's printer silently, which, first of all, means using a printer (which may or may not even exist) without the user's consent; and then imagine that the user figured out why it happens. I doubt the user would use your site again.
Set aside Web, even in a desktop application, nothing should print silently: 1) printer belongs to the user, not to your application; 2) printing should always be a rare event; as computing paves its way to the society, people print less and save paper and toner; deliberately forcing the user to print is a kind of crime.
Moreover, PDF is not a Web content; it is not a part of W3 standard. Don't be confused by the fact that browser's often show PDF as a Web page; this is all non-standard feature. Therefore, for PDF, the best strategy is simple provide a PDF file in a usual HTML anchor. The user knows better what to do with it. Printing of that resource is the work for a PDF software, which you don't have and don't control; only the user manages this stuff.—SA
你似乎错过了一个概念。这可以在您的开发机器上正常工作和打印。当您将此Web应用程序部署到生产Web服务器时,它将不起作用,因为ASP.NET代码完全在服务器端运行,从不在客户端上运行,并且无法访问连接到客户端计算机的任何打印机。
除非你有一台连接到网络服务器的打印机(我严重怀疑它并建议不要做这样的事情),你不能用这段代码打印任何东西。
You seem to be missing a concept. This works and prints fine on YOUR DEVELOPMENT MACHINE. When you deploy this web application to a production web server, it will NOT work as ASP.NET code runs entirely server-side, NEVER on the client, and will NOT have access to any printers attached to the client machine.
Unless you have a printer connected to the web server (I seriously doubt it and recommend against doing such a thing), you cannot print anything using this code.