Print with a printer that is not the one configured by default

FABIAN RODRIGO ROMO 106 Reputation points
2020-03-24T15:44:10.53+00:00

Hi.

I'm migrating from winForm to WPF and I have the following code which works very well:

printDialog = new PrintDialog();
if (DialogResult.OK == printDialog.ShowDialog())
{
    try
                {
                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler(PrintImage);
                    pd.PrinterSettings = printDialog.PrinterSettings;
                    pd.Print();
                }
catch
                {
                }
}

Now in wpf it is indicated that there is an error in the line:

pd.PrinterSettings = printDialog.PrinterSettings;

So to test if the rest of the code works I commented it and it works very well, but obviously it always prints on the printer that the PC has configured by default.

I tried to investigate in other threads how to solve this problem and the solution is supposedly the following:

  PrintDocument pd = new PrintDocument();
                    PrinterSettings ps = new PrinterSettings();
                    pd.PrintPage += new PrintPageEventHandler(PrintImage);
                    printDialog.PrintQueue = new PrintQueue(new PrintServer(),"The exact name of my printer");

                    pd.Print();

My question is: How can I get the name of the printer? Before winForms I didn't need to do that, I imagine that printDialog took care of that at the moment that the user chose the printer.

Any comments or suggestions are welcome.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
{count} vote

Accepted answer
  1. FABIAN RODRIGO ROMO 106 Reputation points
    2020-03-25T10:38:53.05+00:00

    I already got it, in wpf it is like this:

    PrintDocument pd = new PrintDocument();
    PrinterSettings ps = new PrinterSettings();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);
    
    pd.PrinterSettings.PrinterName = printDialog.PrintQueue.Name; 
    
    pd.Print();
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-03-25T08:36:23.107+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    add namespace

    using PrintDialog = System.Windows.Forms.PrintDialog;
    

    then use your code

      PrintDialog printDialog = new PrintDialog();
                if (System.Windows.Forms. DialogResult.OK == printDialog.ShowDialog())
                {
                    try
                    {
                        PrintDocument pd = new PrintDocument();
                        pd.PrintPage += new PrintPageEventHandler(PrintImage);
                        pd.PrinterSettings = printDialog.PrinterSettings;
                        pd.Print();
                    }
                    catch
                    {
                    }
                }
    

    Thanks.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.