Best way to print using wpf 4

Howdy, I need to be able to print wpf from my application. I'm just trying to print a transaction receipt.

I found that using

PrintDialog pDialog = new PrintDialog(); 

pDialog.PrintVisual (new receipt ("transaction name", "my store"), "documentTitle");

Does the trick differ. "Receipt () is a user control that displays transaction details.

How are you going to do this? Is this the right way? What should I do if I do not know the printer to be used? should I make usercontrol only as wide as a printer with a thermal printer?

Any suggestions would be great!

+4
source share
2 answers

This is what I do to print a WPF control:

 System.Windows.Controls.PrintDialog printDlg = new System.Windows.Controls.PrintDialog(); if (printDlg.ShowDialog() == true) { System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket); double scale = Math.Min( capabilities.PageImageableArea.ExtentWidth / control.ActualWidth, capabilities.PageImageableArea.ExtentHeight / control.ActualHeight); control.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale); Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); control.Measure(sz); control.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); printDlg.PrintVisual(control, "My App"); } 

This seems to work well.

+4
source

If you do not know the printer to be used and want to ask the user, does this mean that you want to show the print dialog? What about:

 pDialog.ShowDialog(); 

http://msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.showdialog.aspx

0
source

Source: https://habr.com/ru/post/1335166/


All Articles