【发布时间】: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