繁体   English   中英

如何从打印机打印份数

[英]How to print no of copies from printer

我正在创建一个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);

你能告诉我我在做什么错误或我需要做什么。 感谢您的评论和答复。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM