打印pdf文件,而无需使用Windows表单应用程序打开打印对话框



我想从Windows应用程序打印PDF文件(url(,而无需打开打印对话框。

我已经尝试过下面的代码

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

但在p.Start((中出现错误; 系统.组件模型.Win32异常:没有应用程序与此操作的指定文件相关联

缺少什么?

请建议如何解决这个问题。

我的观点是您应该使用任何第三方库来打印PDF。 我们正在使用以下 C# PDF 库 https://pdfium.patagames.com/c-pdf-library/

是的,这是商业图书馆,所以我不知道我是否有权在这里放置链接。

这是在没有任何用户交互的情况下打印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
}

最新更新