【问题标题】:How to remove the "Save Print output as" dialog when printing a txt file in c#在c#中打印txt文件时如何删除“将打印输出另存为”对话框
【发布时间】:2017-04-26 21:18:39
【问题描述】:

你好 stackoverflow 社区,我正在开发一个简单的 windows 窗体应用程序,它在特定目录中有一个监听器,监听一个 txt 文件,如果监听器检测到一个新文件,它会自动将 txt 文件发送到本地默认打印机,但它也会显示一个“将打印输出另存为”对话框,我需要即时打印过程,而无需与任何对话框交互。

为此,我使用当前命名空间“使用 System.Drawing.Printing;使用 System.IO;”我已经看到了 Print() 方法的定义,但似乎代码受到保护,所以我无法访问删除“将打印输出另存为”对话框。有什么想法吗?

这是我的代码...

fileWatcher:

private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
    try
    {
        MyPrintMethod(e.FullPath);
    }
    catch (IOException)
    {
    }
}

我的打印方法:

private void MyPrintMethod(string path)
{
    string s = File.ReadAllText(path);
    printDocument1.PrintController = new StandardPrintController();
    printDocument1.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
    {
        e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, printDocument1.DefaultPageSettings.PrintableArea.Width, printDocument1.DefaultPageSettings.PrintableArea.Height));

    };
    try
    {
        printDocument1.Print();
    }
    catch (Exception ex)
    {
        throw new Exception("Exception Occured While Printing", ex);
    }
}

【问题讨论】:

  • 我看不到您指定要打印到的实际打印机。
  • 试试这个链接的想法或做谷歌搜索 - stackoverflow.com/questions/10572420/…
  • printDocument1 在哪里定义?我刚刚复制并粘贴了您的代码,但我必须添加var printDocument1 = new PrintDocument(); 才能编译它,并且它可以很好地打印到我的默认打印机上,没有对话框。
  • @RufusL 我从打印工具箱中拖动了对象,如果这样做,则无需使用代码定义它,Visual Studio 会为您完成。在您的情况下,您正确地添加了 var 来定义它。真的没有对话框就打印出来了吗?
  • @MethodMan 其实我用的是他的方案,你可以在“MyPrintMethod”中看到,将printController定义为StandarPrintController,但是还是弹出了保存打印输出对话框。

标签: c# winforms printing file-watcher


【解决方案1】:

当使用的打印机是文档编写器时,会出现该对话框,例如 Microsoft XPS Document WriterMicrosoft Print to PDF。由于您没有按名称指定打印机,因此问题很可能是这是当前的默认打印机。

如果您知道要使用的打印机的名称,则可以这样指定:

printDocument1.PrinterSettings.PrinterName = 
    @"\\printSrv.domain.corp.company.com\bldg1-floor2-clr";

如果您不知道名称,那么您能做的最好的办法就是询问用户他们想打印到哪一个。您可以像这样获取已安装打印机的列表:

var installedPrinters = PrinterSettings.InstalledPrinters;

然后选择一个,您可以在第一个代码示例中指定名称。以下代码可用于提示用户选择打印机,并将打印机设置为他们选择的打印机:

Console.WriteLine("Please select one of the following printers:");
for (int i = 0; i < installedPrinters.Count; i++)
{
    Console.WriteLine($" - {i + 1}: {installedPrinters[i]}");
}

int printerIndex;
do
{
    Console.Write("Enter printer number (1 - {0}): ", installedPrinters.Count);
} while (!int.TryParse(Console.ReadLine(), out printerIndex)
         || printerIndex < 1 
         || printerIndex > installedPrinters.Count);

printDocument1.PrinterSettings.PrinterName = installedPrinters[printerIndex - 1];

【讨论】:

  • 谢谢您,我会尝试您的解决方案,如果可行,我会选择您的答案作为最佳答案;)
  • 那么如果我理解这一点,您需要指定打印机吗?
  • 你好安德鲁,我的问题是在我的电脑中,我没有安装任何打印机,所以 windows 选择以下“Microsoft XPS Document Writer 或 Microsoft Print to PDF”之一。所以这些选项会提示用户保存打印输出。我在另一台安装了物理打印机的计算机上试过这个,效果很好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多