我已经重写了这个问题的答案,因为我找到了一种更好的方法来打印一组流程文档,而只显示一次打印对话框。答案来自麦克唐纳,C 2008中的专业WPF(2008年APRESS),第20章,第704页。
我的代码将一组note对象捆绑到一个名为notestoprint的IList中,并从我的应用程序中的documentServices类获取每个note的流程文档。它设置流程文档边界以匹配打印机,并设置1英寸的边距。然后,它使用文档的documentpaginator属性打印流程文档。代码如下:
// Show Print Dialog
var printDialog = new PrintDialog();
var userCanceled = (printDialog.ShowDialog() == false);
if(userCanceled) return;
// Print Notes
foreach(var note in notesToPrint)
{
// Get next FlowDocument
var collectionFolderPath = DataStore.CollectionFolderPath;
var noteDocument = DocumentServices.GetFlowDocument(note, collectionFolderPath) ;
// Set the FlowDocument boundaries to match the page
noteDocument.PageHeight = printDialog.PrintableAreaHeight;
noteDocument.PageWidth = printDialog.PrintableAreaWidth;
// Set margin to 1 inch
noteDocument.PagePadding = new Thickness(96);
// Get the FlowDocument's DocumentPaginator
var paginatorSource = (IDocumentPaginatorSource)noteDocument;
var paginator = paginatorSource.DocumentPaginator;
// Print the Document
printDialog.PrintDocument(paginator, "FS NoteMaster Document");
}
这是一种非常简单的方法,有一个显著的限制:它不异步打印。要做到这一点,您必须在后台线程上执行这个操作,我就是这样做的。