如何从打印机

时间:2016-07-20 11:24:49

标签: c#-4.0 printing

我正在创建一个Windows程序。我想在用户定义的时间上打印pdf。我做了下面的代码。 我的代码中的问题每次只打印一份,但我希望用户设置他想要打印多少份。

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

你能告诉我我做错了什么或我需要做什么。谢谢你的评论和回答。

1 个答案:

答案 0 :(得分:1)

根据Acrobat文档,您将无法设置份数。

enter image description here

您需要循环才能打印多份副本

您可以尝试以下代码:

    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;
    }