创建打印批处理以打印多个流文档

时间:2009-06-25 18:58:26

标签: c# wpf printing xps

如何批量打印多个flowdocument? 以下代码应打印不同的文档但打印相同。只有在打印到xps打印机时,此示例才能正常工作。

var printDialog = new PrintDialog();
if(printDialog.ShowDialog() == true) {      
  var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);

  foreach(var person in persons) {
    var doc = Application.LoadComponent(new Uri("Template.xaml", UriKind.Relative)) as FlowDocument;
      doc.DataContext = persons;
      var paginator = ((IDocumentPaginatorSource)doc).DocumentPaginator;          
      xpsDocumentWriter.Write(paginator);
    }
  }

1 个答案:

答案 0 :(得分:1)

这些是线程问题。分配DataContext后,确保线程处理其等待队列:

Dispatcher.CurrentDispatcher.Invoke(
        DispatcherPriority.SystemIdle, new DispatcherOperationCallback(_ => null), null);