2012-10-04 78 views
2

我试图使用控制台应用程序在后台打印PDF文档。我使用这个过程来做到这一点。控制台应用程序将PDF文件发送到打印机,但在最小化模式下在后台打开的adobe阅读器出现以下错误:“打开此文档时出错,无法找到该文件”。由于这一次打印多次,我无法杀死这个过程。有没有可能摆脱这个错误? 我的要求是使用过程打印PDF文件,同时这样做的PDF文件必须以最小化模式打开,一旦完成打印阅读器需要自动关闭。我曾尝试下面的代码,但仍引发错误..尝试打印PDF文件时出错

string file = "D:\\hat.pdf"; 
PrinterSettings ps = new PrinterSettings(); 
string printer = ps.PrinterName; 
Process.Start(Registry.LocalMachine.OpenSubKe(@"SOFTWARE\Microsoft\Windows\CurrentVersion"[email protected]"\App Paths\AcroRd32.exe").GetValue("").ToString(),string.Format("/h /t \"{0}\" \"{1}\"", file, printer)); 

回答

0

,因为你想拥有的Acrobat阅读器在后台打开,当你要打印的文档,你可以使用类似:

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

这段代码是从http://aspalliance.com/514_CodeSnip_Printing_PDF_from_NET.all

采取如果你并不需要有Acrobat Reader软件在后台打开,而只是打印像任何其他文件的PDF格式,你可以看看PrintDocument类:

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx