1

I have a WPF application with a list of documents. I have created a print all button, that sends all documents to my default printer. I want to give the user the ability to select a printer, and then send all documents to that printer.

But how do I show the print dialog and save the printer info? And how can I print to a specific printer after closing the dialog?

I have this in my print function, and that works fine (but for the wrong printer)

var p = new Process
{
    StartInfo = new ProcessStartInfo
    {
       CreateNoWindow = true,
       Verb = "print",
       FileName = filePath
    }
};
p.Start();
Ray
  • 45,695
  • 27
  • 126
  • 169
Skywise
  • 429
  • 4
  • 12

3 Answers3

2

Thanks to Ray for lots of help.

The following method works fine for selecting a printer. The printer queue is captured when the user clicks "Print" on the print dialog box.

public PrintQueue SelectPrinter()
{
     var dialog = new PrintDialog();
     if (dialog.ShowDialog() == true)
     {
        if (dialog.PrintQueue != null)
           return dialog.PrintQueue;
     }
     return null;
}

The print queue can then be used when printing multiple documents;

...
var startInfo = new ProcessStartInfo
                        {
                           CreateNoWindow = true,
                           Verb = "printTo",
                           FileName = filePath,
                           Arguments = printQueue.FullName, // <-- here
                           WindowStyle = ProcessWindowStyle.Hidden,
                           UseShellExecute = true,
                        };
var p = Process.Start(startInfo);
...
Skywise
  • 429
  • 4
  • 12
1

You could to use the PrintDialog

A common usage pattern would be

PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() == true)
{
    dialog.PrintVisual(visual, "Job Name");
    //dialog.PrintDocument(paginator, "Document Name");

}

If you want to print from a file you'll need to load the file and create a DocumentPaginator. How to do that depends on the file format you're trying to print.

Ray
  • 45,695
  • 27
  • 126
  • 169
  • Ok, is the paginator solution better than using ProcessStartInfo with Verb="printTo"? – Skywise Dec 20 '11 at 11:22
  • Depends what you mean by better. It lets you do more, like do Print Previews etc, and it allows the user to set the print options (which is what you need). – Ray Dec 20 '11 at 11:32
  • Ok, thanks Ray. Print options are nice. I guess preview is kind of problematic with more than one document. Also, my simple solution seems to often leave Acrobat reader open after printing the last document. I'll look into the paginator. – Skywise Dec 20 '11 at 11:50
  • Oh PDF :( That's not going to be fun. You'll probably need some third party component, so it might not be worth it. – Ray Dec 20 '11 at 12:02
  • You're quite right, it's no fun at all. If I just send them all to the printer using the PrintTo verb, one document seem to be opened in acrobat before the previous is printed. I can make it wait, but when do I know when acrobat is finished printing? – Skywise Dec 21 '11 at 13:15
  • Actually, Windows manages this quite well -- when selecting a bunch of PDFs, right clicking and doing Print, **all** documents are sent quite silently to the printer. Acrobat is indeed opened, and it does remain open after print, but the whole process is silent and works. Mine doesn't. How do they do it? – Skywise Dec 21 '11 at 13:17
  • Added another [question](http://stackoverflow.com/questions/8590998/how-does-windows-bulk-print-pdf-documents) – Skywise Dec 21 '11 at 15:17
0

This is only a clue and not a complete answer but I think it could help.

You can list printers and change the default printer using the windows registry.

Look here and here.

You can read and write in the registry using .NET framework in a easy way.

jlvaquero
  • 8,571
  • 1
  • 29
  • 45