C# 尝试打印pdf文件时出错
我试图使用控制台应用程序在后台打印pdf文档。我用这个过程来做这件事。控制台应用程序将pdf文件发送到打印机,但在后台以最小化模式打开的adobe reader抛出以下错误“打开此文档时出错。找不到此文件”。因此,在多次打印时,我无法终止该进程。有没有可能消除这个错误? 我的要求是使用过程打印pdf文件,同时pdf文件必须以最小化模式打开,打印完成后,阅读器需要自动关闭。我尝试了以下代码,但仍然抛出错误C# 尝试打印pdf文件时出错,c#,.net,acrobat,C#,.net,Acrobat,我试图使用控制台应用程序在后台打印pdf文档。我用这个过程来做这件事。控制台应用程序将pdf文件发送到打印机,但在后台以最小化模式打开的adobe reader抛出以下错误“打开此文档时出错。找不到此文件”。因此,在多次打印时,我无法终止该进程。有没有可能消除这个错误? 我的要求是使用过程打印pdf文件,同时pdf文件必须以最小化模式打开,打印完成后,阅读器需要自动关闭。我尝试了以下代码,但仍然抛出错误 string file = "D:\\hat.pdf"; PrinterSettings
string file = "D:\\hat.pdf";
PrinterSettings ps = new PrinterSettings();
string printer = ps.PrinterName;
Process.Start(Registry.LocalMachine.OpenSubKe(@"SOFTWARE\Microsoft\Windows\CurrentVersion"+@"\App Paths\AcroRd32.exe").GetValue("").ToString(),string.Format("/h /t \"{0}\" \"{1}\"", file, printer));
由于您希望在打印文档时在后台打开Acrobat reader,因此可以使用以下方法:
private static void RunExecutable(string executable, string arguments)
{
ProcessStartInfo starter = new ProcessStartInfo(executable, arguments);
starter.CreateNoWindow = true;
starter.RedirectStandardOutput = true;
starter.UseShellExecute = false;
Process process = new Process();
process.StartInfo = starter;
process.Start();
StringBuilder buffer = new StringBuilder();
using (StreamReader reader = process.StandardOutput)
{
string line = reader.ReadLine();
while (line != null)
{
buffer.Append(line);
buffer.Append(Environment.NewLine);
line = reader.ReadLine();
Thread.Sleep(100);
}
}
if (process.ExitCode != 0)
{
throw new Exception(string.Format(@"""{0}"" exited with ExitCode {1}. Output: {2}",
executable, process.ExitCode, buffer.ToString());
}
}
您可以通过将上述代码合并到项目中并按如下方式使用来打印PDF:
string pathToExecutable = "c:\...\acrord32.exe";
RunExecutable(pathToExecutable, @"/t ""mytest.pdf"" ""My Windows PrinterName""");
此代码取自
如果不需要在后台打开Acrobat Reader,只需像打印其他文档一样打印pdf,则可以查看PrintDocument类:
由于您希望在打印文档时在后台打开Acrobat reader,因此可以使用以下方法:
private static void RunExecutable(string executable, string arguments)
{
ProcessStartInfo starter = new ProcessStartInfo(executable, arguments);
starter.CreateNoWindow = true;
starter.RedirectStandardOutput = true;
starter.UseShellExecute = false;
Process process = new Process();
process.StartInfo = starter;
process.Start();
StringBuilder buffer = new StringBuilder();
using (StreamReader reader = process.StandardOutput)
{
string line = reader.ReadLine();
while (line != null)
{
buffer.Append(line);
buffer.Append(Environment.NewLine);
line = reader.ReadLine();
Thread.Sleep(100);
}
}
if (process.ExitCode != 0)
{
throw new Exception(string.Format(@"""{0}"" exited with ExitCode {1}. Output: {2}",
executable, process.ExitCode, buffer.ToString());
}
}
您可以通过将上述代码合并到项目中并按如下方式使用来打印PDF:
string pathToExecutable = "c:\...\acrord32.exe";
RunExecutable(pathToExecutable, @"/t ""mytest.pdf"" ""My Windows PrinterName""");
此代码取自
如果不需要在后台打开Acrobat Reader,只需像打印其他文档一样打印pdf,则可以查看PrintDocument类: