以编程方式打印 FixedDocument [英] Print FixedDocument programmatically
问题描述
我将 WPF FixedDocument 与数据绑定用于简单的发票报告.在软件本身内部查看时工作完美.
I am using a WPF FixedDocument with databinding for a simple invoice report. Works perfect when viewed inside the sofware itsself.
但我想一键打印一系列发票.当我选择 XPS 编写器时,以下代码完美运行(快速不脏,只需直接在视图模型中一张一张地加载发票,用于测试目的),当我选择 XPS 编写器时,无法正确打印到真正的打印机.我看不到任何绑定到报告的数据.线条等所有图形元素都在那里,但没有数据.(当我使用相同的按钮打印到 de xps writer 打印机时,所有数据都存在并且正确......)
But i want to print a series of invoices in one click. The following code works perfect (quick 'n dirty, just loads an invoice one by one directly inside the viewmodel, for testing purposes) when I choose the XPS writer, bu fails to print correctly when printing to a real printer. I can see nothing of the data bound to the report. All the graphical elements such as lines are there, but no data. (When i print, with the same button, to de xps writer printer, all data is present, and correct...)
有什么想法吗?
private void ExecutePrintCommand(object sender, ExecutedRoutedEventArgs args)
{
var invs = args.Parameter as IList<object>;
using (CompuDataContext db = new CompuDataContext())
{
DataLoadOptions dl = new DataLoadOptions();
dl.LoadWith<Invoice>(f => f.Invoicelines);
db.LoadOptions = dl;
ReportViewer viewer = new ReportViewer();
PrintDialog dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
PrintQueue q = dlg.PrintQueue;
foreach (var o in invs)
{
InvoiceListDisplay inv = o as InvoiceListDisplay;
Invoice invoice = db.Invoices.Single(f => f.Id == inv.Id);
viewer.DataContext = new InvoicePrintViewModel(invoice);
XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(q);
xpsdw.Write(viewer.Document);
}
}
}
}
推荐答案
mmkay,所以我自己在这里找到了答案 :)
mmkay, so I found the answer myself here :)
这对我有帮助(有人知道背后的原因"是什么吗?错误?)
This helped me (Anybody an idea what the 'reason' behind is? Bug?)
PS:在流程文档中,我遇到了同样的问题,但无法在那里解决.有什么想法吗?
PS: In a flowdocument, i experience the same issue, and have not been able to resolve it there. Any ideas?
这篇关于以编程方式打印 FixedDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!