0

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.

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?

Here is my code...

The fileWatcher:

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

My print method:

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);
    }
}
  • I cant see you specifying the actual printer to print to. – Andrew Harris Apr 26 '17 at 21:39
  • try this link for ideas or do a google search - http://stackoverflow.com/questions/10572420/how-to-skip-the-dialog-of-printing-in-printdocument-print-and-print-page-direc – MethodMan Apr 26 '17 at 21:39
  • Where is `printDocument1` defined? I just copied and pasted your code, but I had to add `var printDocument1 = new PrintDocument();` for it to compile, and it printed fine to my default printer here at work with no dialogs. – Rufus L Apr 26 '17 at 21:41
  • @RufusL I dragged the object from the printing toolBox, if you do that, you don't need to define it with code, visual studio does that for you. In your case, you did right with adding var to define it. Did it really printed without the dialog? – Sergio Molina Apr 26 '17 at 21:53
  • @MethodMan Actually I used his solution, you can see it in "MyPrintMethod", defining the printController as StandarPrintController, but still pops up a save print output dialog. – Sergio Molina Apr 26 '17 at 21:57
  • @AndrewHarris the print class sends the document to the default printer in Windows. The idea is that the user doesn't have to interact with any config dialog or save dialog, the user needs to download a file to a specific directory and the app will do the printing proccess automaticly – Sergio Molina Apr 26 '17 at 21:59
  • Yes, because my default printer was a network printer. Probably yours is a document writer? Check my answer and see if it helps... – Rufus L Apr 26 '17 at 22:18

1 Answers1

2

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];
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Thank you, I will try your solution and if it works, I will select your answer as the best answer ;) – Sergio Molina Apr 26 '17 at 22:22
  • So if I understand this, you needed to specify the printer? – Andrew Harris Apr 27 '17 at 07:36
  • Hellow Andrew, my problem was that in my computer, I don't have any printer installed, so windows selects 1 of the following "Microsoft XPS Document Writer or Microsoft Print to PDF". So those options prompts the user to save the print output. I tried this in another computer with a physical printer installed and it worked perfectly! – Sergio Molina Apr 28 '17 at 05:47