2016-07-20 3 views
0

私はWindowsプログラムを作成しています。私は、ユーザーが定義した時間数でpdfを印刷したいと思っています。私は以下のコードを実行しました。 私のコードの問題はたびに1つのコピーを印刷するだけですが、ユーザーが印刷するコピーの数を設定する必要があります。プリンタからコピーを印刷する方法

int NumOfLabel = 10; /* here for example i set to 10 copy */ 
Process objProcess1 = new Process(); 
FileName = @"D:\Project\Document\2320.pdf"; 
//Print the file 
PrintDialog pdi = new PrintDialog(); 
pdi.PrinterSettings.Copies = (short)NumOfLabel; 
if (pdi.ShowDialog() == DialogResult.OK) 
{ 
    PrinterName = pdi.PrinterSettings.PrinterName; 
/// Set the printer. 
AddPrinterConnection(PrinterName); 
SetDefaultPrinter(PrinterName); 
ProcessStartInfo info = new ProcessStartInfo(); 
info.Verb = "Print"; 
info.FileName = FileName; 
info.CreateNoWindow = true; 
info.WindowStyle = ProcessWindowStyle.Normal; 
info.UseShellExecute = true; 
objProcess1.StartInfo = info; 
objProcess1.Start(); 
MessageBox.Show("Print done."); 
} 

//Add the printer connection for specified pName. 
[DllImport("winspool.drv")] 
    private static extern bool AddPrinterConnection(string pName); 
//Set the added printer as default printer. 
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool SetDefaultPrinter(string Name); 

私は間違いをしているか、何をする必要があるか教えてください。あなたのコメントと答えをありがとう。

+0

:あなたは、次のコードを試すことができますコピー

の多数を印刷するためにあなたがループする必要

enter image description here

MaximumCopiesプロパティを使用して、プリンタがサポートする最大コピー数を決定します。プリンタでサポートされている最大コピー数を超えて設定すると、最大コピー数のみが出力され、例外は発生しません。 - [source](https://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.copies(v = vs.110).aspx) – ChrisF

+1

印刷しようとしていることに注意してくださいPDFファイルに10ページありましたか?代わりに10回印刷したい場合があります – ehh

+0

プリンタが複数のコピーをサポートしていることを確認します。それがループしなければならない場合はループする必要があります – ChrisF

答えて

1

Acrobatのドキュメントによれば、印刷部数を設定することはできません。 。「すべてのプリンタは、あなたが使用することができ、印刷複数の対処サポートするわけではありません

public static void Print(string pdfFileName, string printerName, int copies) 
    { 
     if (File.Exists(pdfFileName)) 
     { 
      const string flagNoSplashScreen = "/s"; 
      const string flagOpenMinimized = "/h"; 

      var acrobatReaderApplicationPath = GetAcrobatReaderApplicationPath(); 
      if (acrobatReaderApplicationPath == null) 
      { 
       throw new AcrobatNotInstalledException(); 
      } 

      var flagPrintFileToPrinter = string.Format("/t \"{0}\" \"{1}\"", pdfFileName, printerName); 
      var args = string.Format("{0} {1} {2}", flagNoSplashScreen, flagOpenMinimized, flagPrintFileToPrinter); 

      for (int i = 0; i < copies; i++) 
      { 
       using (var process = new Process()) 
       { 
        var startInfo = new ProcessStartInfo 
        { 
         FileName = acrobatReaderApplicationPath, 
         Arguments = args, 
         CreateNoWindow = true, 
         Verb = "Print", 
         ErrorDialog = false, 
         UseShellExecute = false, 
         WindowStyle = ProcessWindowStyle.Hidden 
        }; 
        process.StartInfo = startInfo; 
        process.Start(); 

        if (process != null) 
        { 
         process.WaitForInputIdle(); 
         process.CloseMainWindow(); 
        } 
       } 
      } 
     } 
    } 

private static string GetAcrobatReaderApplicationPath() 
    { 
     string applicationPath; 

     var printApplicationRegistryPaths = new[] 
     { 
      @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRD32.exe", 
      @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Acrobat.exe" 
     }; 

     foreach (var printApplicationRegistryPath in printApplicationRegistryPaths) 
     { 
      using (var regKeyAppRoot = Registry.LocalMachine.OpenSubKey(printApplicationRegistryPath)) 
      { 
       if (regKeyAppRoot == null) 
       { 
        continue; 
       } 

       applicationPath = (string)regKeyAppRoot.GetValue(null); 

       if (!string.IsNullOrEmpty(applicationPath)) 
       { 
        return applicationPath; 
       } 
      } 
     } 

     return null; 
    } 
+0

このコードはありがたいですが、マシンにはAcrobat Readerがなくても、このコードはうまくいかないでしょう。 –

+0

それで、シーン? Acrobat Readerを使用していると確信していました。 – ehh

+0

おそらくシステムはcutePDFを持っていて、このように多くのものがたくさんあります。 –

関連する問題