WPF:在没有打印对话框的情况下打印 FlowDocument

发布于 2024-09-16 08:56:47 字数 487 浏览 10 评论 0原文

我正在 WPF 中编写一个笔记应用程序,为每个单独的笔记使用 FlowDocument。该应用程序按标签搜索和过滤笔记。我想将当前过滤列表中的所有注释打印为单独的文档,并且只想在作业开始时显示一个打印对话框。

我在这个线程中找到了一个很好的打印示例,但它适合打印单个FlowDocument,因此它使用 CreateXpsDocumentWriter() 重载来显示打印对话框。

所以,这是我的问题:任何人都可以建议一些好的代码来打印 FlowDocument 而不显示 PrintDialog 吗?我想我将在过程开始时显示“打印对话框”,然后循环浏览我的笔记集合以打印每个 FlowDocument

I am writing a note-taking application in WPF, using a FlowDocument for each individual note. The app searches and filters notes by tags. I want to print all notes in the current filtered list as separate documents, and I only want to show a single Print Dialog at the beginning of the job.

I found a good print example in this thread, but it is geared toward printing a single FlowDocument, so it uses a CreateXpsDocumentWriter() overload that displays a Print Dialog.

So, here's my question: Can anyone suggest some good code for printing a FlowDocument without displaying a PrintDialog? I figure I'll display the Print Dialog at the beginning of the procedure and then loop through my notes collection to print each FlowDocument.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-09-23 08:56:47

我重写了这个问题的答案,因为我确实找到了一种更好的方法来打印一组 FlowDocuments,同时仅显示一次打印对话框。答案来自 MacDonald,Pro WPF in C# 2008 (Apress 2008) 第 20 章,第 20 页。 704.

我的代码将一组 Note 对象捆绑到一个名为notesToPrint 的 IList 中,并从我的应用程序中的 DocumentServices 类获取每个 Note 的 FlowDocument。它设置 FlowDocument 边界以匹配打印机并设置 1 英寸边距。然后,它使用文档的 DocumentPaginator 属性打印 FlowDocument。代码如下:

// 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");
}

这是一种非常简单的方法,有一个重大限制:它不异步打印。为此,您必须在后台线程上执行此操作,这就是我的做法。

I have rewritten my answer to this question, because I did find a better way to print a set of FlowDocuments, while showing the Print Dialog only once. The answer comes from MacDonald, Pro WPF in C# 2008 (Apress 2008) in Chapter 20 at p. 704.

My code bundles a set of Note objects into an IList called notesToPrint and gets the FlowDocument for each Note from a DocumentServices class in my app. It sets the FlowDocument boundaries to match the printer and sets a 1-inch margin. Then it prints the FlowDocument, using the document's DocumentPaginator property. Here's the code:

// 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");
}

This is a pretty simple approach, with one significant limitation: It doesn't print asynchronously. To do that, you would have to perform this operation on a background thread, which is how I do it.

坦然微笑 2024-09-23 08:56:47

获得 printDialog 后只需一个循环。

for(int i=0 i<document.count i++)
    printdocument((document[i] as iDocumentPaginator),"title"+[i]);

Just a loop after you have got the printDialog.

for(int i=0 i<document.count i++)
    printdocument((document[i] as iDocumentPaginator),"title"+[i]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文