2012-03-02 138 views
0

我正在使用c#封装来批量打印PDF文件,而无需用户交互。我正在使用一个由c#wrapper动态创建的ps文件,并提供该ps文件作为进程启动参数。使用ps文件的原因是gs不支持动态用户友好假脱机程序名称作为命令行参数。他的问题是,当我使用ps文件时,gs不会自动选择打印机。它总是要求手动选择打印机。这里是代码ghostscript命令行参数ps文件中的打印机选择

class Program 
{ 
    static void Main(string[] args) 
    { 


     try 
     { 
      string path = Application.StartupPath + "\\wd.print"; 
      StreamReader sr = new StreamReader(path); 
      string[] content = sr.ReadToEnd().Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); 
      sr.Close(); 

      string printer = content[0]; 

      for (int i = 1; i < content.Length; ++i) 
      { 
       ProcessStartInfo psInfo = new ProcessStartInfo(); 


       psInfo.Arguments = String.Format(" setup.ps -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sOutputFile=\"\\\\spool\\{0}\" \"{1}\"", 
        printer, 
        content[i]); 


       String spoolerName = content[i].Substring(content[i].IndexOf("$")+1); 
       //creates the dynamic ps file named setup.ps 
       generateSettings(spoolerName); 
       string gs = Application.StartupPath + @"\gs\gswin32c.exe"; ; 
       psInfo.FileName = gs; 
       psInfo.UseShellExecute = false; 
       Process process = Process.Start(psInfo); 
       process.WaitForExit(); 
       Console.ReadLine(); 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      Console.Write(ex.Message); 
      Console.ReadLine(); 
     } 
    } 
    protected static void generateSettings(String name) 
    { 
     //the code which creates the dynamic ps file named setup.ps 
     FileStream file=File.Create("setup.ps"); 
     StreamWriter writer = new StreamWriter(file); 
     writer.WriteLine("mark"); 
     writer.WriteLine(" /NoCancel true "); 
     writer.WriteLine(" /UserSettings"); 
     writer.WriteLine(" <<"); 
     writer.WriteLine(" /DocumentName (" + name + ")");//custom name for windows print spooler 
     writer.WriteLine(" >>"); 
     writer.WriteLine(" (mswinpr2) finddevice"); 
     writer.WriteLine(" putdeviceprops"); 
     writer.WriteLine("setdevice"); 

     writer.Flush(); 
     writer.Close(); 
     file.Close(); 




    } 
} 

请帮助我。我想自动选择打印机,因此它不需要用户干预

回答

0

为什么不创建.PS文件,然后通过以下机制将数据发送到您选择的打印机:How to send raw data to a printer。您可以获取打印机列表,以编程方式选择它,然后将.PS发送到设备。发送文件时,您可以指定假脱机程序/作业名称中显示的内容。

+0

欢迎来到Stackoverflow Dasun .. – 2013-05-20 11:11:02

相关问题