Best method to print using wpf 4
Howdy, I need to be able to print from my wpf application. I am just trying to print a transaction receipt.
I have found that using
PrintDialog pDialog = new PrintDialog();
pDialog.PrintVisual(new Receipt("transaction name","my store"), "documentTitle");
Does the trick very nicely. "Receipt() is a usercontrol that rend开发者_运维技巧ers out the transaction details.
How are you meant to do this? is this the correct way? what do I do if I dont know the printer that is going to be used? should I make the usercontrol only as wide as a thermal receipt printer?
Any suggestions would be great!
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 nicely.
If you don't know the printer that is going to be used and want to ask the user, does that mean you want to show the print dialog? How about:
pDialog.ShowDialog();
http://msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.showdialog.aspx
精彩评论