最好的方法使用WPF 4打印 [英] Best method to print using wpf 4
问题描述
你好, 我需要能够从我的WPF应用程序打印。我只是想打印交易收据。
Howdy, I need to be able to print from my wpf application. I am just trying to print a transaction receipt.
我发现,使用
PrintDialog pDialog = new PrintDialog();
pDialog.PrintVisual(新收据(事务名称,我的店),documentTitle);
pDialog.PrintVisual(new Receipt("transaction name","my store"), "documentTitle");
的伎俩非常漂亮。 收据()是一个用户控件呈现出交易详情。
Does the trick very nicely. "Receipt() is a usercontrol that renders 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?
任何建议将是巨大的!
推荐答案
这是我做的打印WPF控件:
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");
}
这似乎很好地工作。
这篇关于最好的方法使用WPF 4打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!