网络打印机忽略以编程方式发送的份数

问题描述

设置 - 本地 Windows 10 PC 安装了多台网络打印机。 GUI C# WinForm 应用程序 (.NET) 在后台持续运行,偶尔会从预定义的 URL(从 *.ini 文件读取)下载 PDF 文件。

打印上述PDF文件时出现问题。打印机不接受应用程序发送的份数,而是只打印一份文件副本。

这是我代码的相关部分:

        string webPrinter = "HP LaserJet PCL 6"; // is set in another part of the code
        string iniFilePrinter = "hp LaserJet 1320 PCL 5"; // is set in another part of the code - read from the ini file
        string dirName = "C:\\mydir";
        string newDocName = "mydoc.pdf";
        short numCopies = 1;

        if(event1 == "event1") {        // taken from another part of the code
            numCopies = webNumCopies;   // taken from another part of the code
        } else if(event2 == "event2") {
            numCopies = iniNumCopies;   // taken from another part of the code - read from the ini file
        }

        var path = dirName + "\\" + newDocName;

        try
        {
            using (var document = PdfiumViewer.PdfDocument.Load(path))
            {
                using (var printDocument = document.CreatePrintDocument())
                {
                    System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();
                    string defaultPrinterName = settings.PrinterName;

                    printDocument.DocumentName = newDocName;
                    printDocument.PrinterSettings.PrintFileName = newDocName;
                    printDocument.PrinterSettings.Copies = numCopies;
                    printDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
                    printDocument.PrinterSettings.PrinterName = webPrinter;

                    MessageBox.Show("Before: " + printDocument.PrinterSettings.Copies.ToString() + " --- " + newDocName);

                    if (!printDocument.PrinterSettings.IsValid)
                    {
                        printDocument.PrinterSettings.PrinterName = iniFilePrinter;

                        if(!printDocument.PrinterSettings.IsValid)
                        {
                            printDocument.PrinterSettings.PrinterName = defaultPrinterName;
                        }
                    }

                    MessageBox.Show("After: " + printDocument.PrinterSettings.Copies.ToString() + " --- " + newDocName);

                    printDocument.Print();
                }
            }
        }
        catch (Exception ex) {
            MessageBox.Show(ex.Message);
        }

从未触发过异常,并且每次尝试都打印成功。在满足条件时也会更改 if/else 中的副本数,并且代码的 MessageBox.Show() 部分确实显示了预期的副本数(2,3,7,除了 1,当它不应该1) 在调用 printDocument.Print() 之前立即。

我还尝试从其他各种程序(MS Word、各种自定义应用程序、PDF 阅读器等)打印不相关的文档,副本数始终为 1。但是,Google Chrome 或 FireFox 等软件设法打印指定份数的东西。

我在想可能是打印机设置有问题,导致它忽略发送的份数。基于这个假设,我检查了所有打印机的设置,发现份数实际上设置为 1。

如果这确实是我的问题的原因,我该如何绕过该设置(而不实际更改它),就像 Google Chrome 和 Firefox 似乎能够做到的那样?我知道我可能会以编程方式更改该限制(将其设置为我的份数,然后在打印完成后将其更改回原始值),但这似乎不是正确的做法.

编辑

我通过包含一个打印对话框来扩展我的代码,如下所示:

                PrintDialog printDlg = new PrintDialog();
                printDlg.Document = printDocument;
                printDlg.AllowSelection = true;
                printDlg.AllowSomePages = true;

                if (printDlg.ShowDialog() == DialogResult.OK)
                {
                    printDocument.Print();
                }

然而,结果是相同的 - 即使用户在打印对话框中更改了份数,打印机也会忽略它们。在另一台(本地)打印机上测试了相同的代码,该打印机连接到一台无关的 Windows 10 PC,并且对话框中的副本数量没有被忽略。

我还注意到我的应用程序中的打印对话框和 notepad.exe 中的打印对话框不同(下图)。有没有办法让我调用 notepad.exe 使用的相同打印对话框?我想这样做的原因是因为那个人完成了工作(打印对话框中的 xy 份数,打印的 xy 份数)。

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...