1
votes

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();
3

3 Answers

2
votes

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);
...
1
votes

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.

0
votes

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.