简体   繁体   English

从asp.net应用程序静默打印pdf文件

[英]Print pdf file silently from asp.net application

I searched Google and found couple of solution. 我搜索了Google,找到了几种解决方案。 I used the following code. 我用下面的代码。 It can print to my network printer when application runs through Visual Studio with IIS Express but when I hosted application to my local host the following code does not print any thing. 当应用程序通过带有IIS Express的Visual Studio运行时,它可以打印到我的网络打印机,但是当我将应用程序托管到本地主机时,以下代码无法打印任何内容。

ProcessStartInfo psi = new ProcessStartInfo(pdfFileName) { Verb = "Print" };
            psi.CreateNoWindow = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            Process p = new Process();
            p.StartInfo = psi;
            Process.Start(psi);
            System.Threading.Thread.Sleep(5000);
            if (false == p.CloseMainWindow())
            {
                p.Kill();
            }
            else
            {
                p.WaitForInputIdle();
            }

I am totally stuck. 我完全被困住了。 Please give me suggestion or give me either sample code or links. 请给我建议或给我示例代码或链接。

If I'm understanding this correctly, you're starting a new process with the filename of the PDF, and trying to pass a verb into the process so it knows what to do (to print the PDF). 如果我正确理解了这一点,则说明您正在使用PDF的文件名开始一个新的过程,并尝试将动词传递到该过程中,以便它知道要做什么(打印PDF)。

I would comment out the "CreateNoWindow" part and see what happens. 我将注释掉“ CreateNoWindow”部分,看看会发生什么。 Do you have anything on the server that is capable of opening and displaying PDF files? 服务器上是否有任何可以打开和显示PDF文件的东西? PDF's could be handled by Edge, Adobe Reader, Reader application (on 8.1), IrfanView, etc etc, and those applications will no doubt handle the "Print" verb differently. PDF可以由Edge,Adobe Reader,Reader应用程序(8.1版),IrfanView等处理,这些应用程序无疑将以不同的方式处理“ Print”动词。

Your sleep function will also produce inconsistent results because it is waiting for a specific amount of time. 您的睡眠功能也会在等待特定时间后产生不一致的结果。 You might have better luck with something like WinWaitActive from the AutoItX toolkit (as you are kind-of doing UI automation). 使用AutoItX工具包中的WinWaitActive之类的东西可能会更好一些(因为您正在做UI自动化)。

Also, instead of doing 另外,不要做

if (false == p.CloseMainWindow())

you can do 你可以做

if (!p.CloseMainWindow())

This can improved code readability (The ! is essentially the same as "not", so its checking if that returned value is Not True). 这样可以提高代码的可读性(!本质上与“ not”相同,因此它检查返回的值是否不是True)。

EDIT: Other considerations is that IIS Express would run under your local account which would no doubt have the printers installed. 编辑:其他考虑因素是IIS Express将在您的本地帐户下运行,这无疑会安装打印机。 The full IIS server would likely run as the Network Service account which may not have any printers installed. 完整的IIS服务器可能以网络服务帐户运行,该帐户可能未安装任何打印机。

You might consider it a potentially wiser design decision to maybe do something like, say, have your PDF's generated into a folder, and then have another C# application run on the server as a service to print all files in this folder as they come in. You could run that service as a user who has those printers installed. 您可能认为这样做可能是一个明智的设计决定,例如,将PDF生成到一个文件夹中,然后在服务器上运行另一个C#应用程序作为服务,以在该文件夹中的所有文件进入时将它们打印出来。您可以以安装了那些打印机的用户身份运行该服务。 You could implement a FileSystemWatcher to only trigger when new files are put into that directory, handle the print job, and then delete the PDF. 您可以实现FileSystemWatcher,使其仅在将新文件放入该目录时触发,处理打印作业,然后删除PDF。 See here for how that all works (the file watching bit of it): Using FileSystemWatcher to monitor a directory 请参阅此处以了解所有工作原理(其中的文件监视位): 使用FileSystemWatcher监视目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM