question

FABIANRODRIGOROMO-2314 avatar image
0 Votes"
FABIANRODRIGOROMO-2314 asked ·

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

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-wpf
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

Welcome to our Microsoft Q&A platform!

You can try to add namespace in wpf:

 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.

0 Votes 0 · ·
FABIANRODRIGOROMO-2314 avatar image
0 Votes"
FABIANRODRIGOROMO-2314 answered ·

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();
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.