简体   繁体   English

使用Windows窗体应用程序无需打开“打印对话框”即可打印pdf文件

[英]Print pdf file without opening Print Dialog using windows form application

I want to print a PDF file (url) from windows application without opening print dialog. 我想从Windows应用程序中打印PDF文件(URL)而不打开打印对话框。

I have tried the code bellow 我已经尝试过以下代码

        string pdfUrl="mysite.com/test.pdf";
        string printerName = "Microsoft Print To PDF";
        using (var client = new System.Net.WebClient())
        {
            client.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            client.DownloadFile(pdfUrl, filePath);
        }

        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = filePath;
        info.Arguments = "\"" + printerName + "\"";
        info.UseShellExecute = true;
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden;
        info.WorkingDirectory = Path.GetDirectoryName(filePath);

        Process p = new Process();
        p.StartInfo = info;
        p.Start();

        //p.WaitForInputIdle();
        //System.Threading.Thread.Sleep(3000);
        //if (false == p.CloseMainWindow())
        //    p.Kill();

but getting error in p.Start(); 但是在p.Start()中出现错误 bellow System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation 在以下System.ComponentModel.Win32Exception:没有应用程序与此操作的指定文件相关联

What is missing? 什么东西少了?

Please suggest how to solve this. 请提出解决方法。

My opinion is that you should use any third party library to printing PDFs. 我的意见是,您应该使用任何第三方库来打印PDF。 We are using following C# PDF library https://pdfium.patagames.com/c-pdf-library/ 我们正在使用以下C#PDF库https://pdfium.patagames.com/c-pdf-library/

And yes, this is commercial library, so I don't know if I have a right to place a link here. 是的,这是商业图书馆,所以我不知道我是否有权在此处放置链接。

Here is a code to print PDF's without any user interaction: 这是无需任何用户交互即可打印PDF的代码:

public void PrintPdf()
{
    var doc = PdfDocument.Load("c:\test.pdf");
    var printDoc = new PdfPrintDocument(doc);
    PrintController printController = new StandardPrintController();
    printDoc.PrintController = printController;
    printDoc.Print(); // Print PDF document
}

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

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