如何删除“将打印输出另存为"在 C# 中打印 txt 文件时的对话框 [英] How to remove the "Save Print output as" dialog when printing a txt file in c#
问题描述
你好 stackoverflow 社区,我正在开发一个简单的 windows 窗体应用程序,它在特定目录中有一个侦听器侦听 txt 文件,如果侦听器检测到新文件,它会自动将 txt 文件发送到本地默认打印机,但它也显示了一个将打印输出另存为"对话框,我需要打印过程是即时的,而不必与任何对话框交互.
Hello stackoverflow community, I am developing a simple windows form application that has a listener in a specific directory listening for a txt file, if the listener detects a new file, it will automaticly send the txt file to a local default printer, but it also displays a "Save print output as" dialog and I need the printing proccess to be instant, without having to interact with any dialog.
为此,我使用当前命名空间using System.Drawing.Printing; using System.IO;"我已经看到了 Print() 方法的定义,但似乎代码受到保护,所以我无法删除将打印输出另存为"对话框.有什么想法吗?
To do this I am using the current namespace "using System.Drawing.Printing; using System.IO;" and I have seen the definition of the Print() method but seems like the code is protected so I can't have access to remove the "save print output as" dialog. Any ideas?
这是我的代码...
文件观察者:
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);
}
}
推荐答案
当使用的打印机是文档编写器时出现该对话框,例如 Microsoft XPS Document Writer
或 Microsoft Print to PDF代码>.由于您没有按名称指定打印机,问题很可能是当前的默认打印机.
That dialog appears when the printer being used is a document writer, like Microsoft XPS Document Writer
or Microsoft Print to PDF
. Since you don't specify a printer by name, the problem is likely that this is the current default printer.
如果您知道要使用的打印机的名称,则可以这样指定:
If you know the name of the printer you want to use, then you can specify it like so:
printDocument1.PrinterSettings.PrinterName =
@"\\printSrv.domain.corp.company.com\bldg1-floor2-clr";
如果您不知道名称,那么您能做的最好的事情就是询问用户他们想要打印到哪个名称.您可以获得已安装打印机的列表,如下所示:
If you don't know the name, then probably the best you can do is ask the user which one they want to print to. You can get a list of the installed printers like this:
var installedPrinters = PrinterSettings.InstalledPrinters;
然后选择一个,您可以在第一个代码示例中指定名称.下面是一些代码,可用于提示用户选择打印机,并将打印机设置为他们选择的打印机:
And then when one is chosen, you can specify the name as in the first code sample. Here's some code that you can use to prompt the user for a printer, and set the printer to the one they choose:
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];
这篇关于如何删除“将打印输出另存为"在 C# 中打印 txt 文件时的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!