Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintDialog.PrinterSettings equivalent in WPF

Tags:

c#

printing

wpf

I'm trying to add printing support to a C# WPF application I'm writing and I'm tearing my hair out over this. I'm trying to print a single image from a window in a WPF application. The image is a shipping label and the printer is a thermal printer loaded with 4"x6" shipping label stock. The code to print is as follows:

PrintDialog pd = new PrintDialog();

if (pd.ShowDialog() == true)
{
    Image tmpImage = new Image();
    tmpImage.Stretch = Stretch.Uniform;
    tmpImage.Width = pd.PrintableAreaWidth;
    tmpImage.Source = this.img_label.Source;
    tmpImage.Measure(new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight));
    tmpImage.Arrange(new Rect(new Point(0, 0), tmpImage.DesiredSize));

    pd.PrintVisual(tmpImage, "Shipping Label");
}

This code works in that it will display the print dialog, I can choose my printer, configure it to use the correct label stock, and print the label. However, as other posts have indicated, it does not save the settings I have selected. So, if I choose to print the same image again without closing the application in between, it reverts to the default printer and, even when I choose the correct printer, defaults that printer to the default settings, which includes using the wrong size label stock. So every time I print I have to select the printer and configure it to use the correct stock. This is simply not acceptable in real world use.

After much searching online I have found numerous posts about this but all of them talk about saving the PrintDialog.PrinterSettings object and then using that to initialize the next PrintDialog instance. However, in WPF, there is no PrinterSettings member of the PrintDialog class. That is a Win Forms object. Why the Win Forms and WPF PrintDialog objects are different is beyond me, but that's likely a question that won't get answered. The real question is what I do now. I can, if necessary, re-invent the entire wheel and have my own printer selector and printer configuration pages and print the image using a PrintDocument object and bypass the PrintDialog entirely. I'd rather not do this unless it is entirely necessary. Displaying the PrintDialog is nice, it's what people are used to, and it already has all the ability to configure the printer built right in. But how can I initialize the PrintDialog in WPF to select the proper printer and use the proper printer settings? If only I were using Windows Forms this would be built in. What's the WPF equivalent?

The secondary question is, if there is no WPF equivalent, what is the recommended way to handle this? I don't really need to give the user the ability to configure the printer within my application. All I want it to do is remember the previous settings they chose the next time they go to print, just like every single other PC application that has ever been written. How can this be so hard?

Any help anyone can provide would be greatly appreciated. In the mean time I'm going down the path of re-inventing the proverbial wheel. I hope to get an easier answer soon.

Thanks!

like image 360
Mageician Avatar asked Jul 28 '13 20:07

Mageician


1 Answers

WPF has PrintTicket and PrintQueue classes (and PrintDialog has corresponding properties, which can be initialized with your saved settings).

For the simplicity, you can consider the first one as the paper settings, and the second one - as the printer settings (selected printer).

like image 113
Dennis Avatar answered Sep 23 '22 15:09

Dennis