[英]C# - Print a PDF Error (System.ComponentModel.Win32Exception:...)
I am trying to write an application to print PDFs.我正在尝试编写一个打印 PDF 的应用程序。 Now I get an error message where I can't get any further after a long search.现在我收到一条错误消息,经过长时间的搜索,我无法进一步了解。
The goal would be to print a PDF file without opening the PDF-Reader.目标是在不打开 PDF 阅读器的情况下打印 PDF 文件。
My Code:我的代码:
using System.Diagnostics;
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = @"C:\testFile.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
The error condition occurs at p.Start();错误情况发生在p.Start(); line.线。
Error message:错误信息:
System.ComponentModel.Win32Exception: "The system cannot find the specified file". System.ComponentModel.Win32Exception:“系统找不到指定的文件”。
New experience: 20.01.2021新体验:20.01.2021
If I comment out info.Verb = "print";
如果我注释掉info.Verb = "print";
. . Then the PDF is opened.然后打开 PDF。 Is this a sign that it finds the PDF but has no access to printer?这是否表明它找到了 PDF 但无法访问打印机?
use this code it works for me:使用它对我有用的代码:
public static void PrintToASpecificPirnter()
{
using (PrintDialog printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
var StartInfo = new ProcessStartInfo();
StartInfo.CreateNoWindow = true;
StartInfo.UseShellExecute = true;
StartInfo.Verb = "printTo";
StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.FileName = fileName;
Process.Start(StartInfo);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.