创建非Visual的XPS文档的示例

创建非Visual的XPS文档的示例

本文介绍了WPF-创建非Visual的XPS文档的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找不涉及RDLC/SSRS的报告/打印解决方案.我想使用DocumentViewer,我知道它支持XPS.我已经找到了很多使用Visual到XPS的示例,但是还没有找到可以使用现有WPF页面,带有各种控件(如标签,列表框,网格等)并将其创建到XPS文档中的示例.是否有一个代码示例可以使用整个XAML页面并创建XPS?

I'm looking for a reporting/printing solution that does not involve RDLC/SSRS. I'd like to use the DocumentViewer, which I know supports XPS. I have found plenty of examples that use Visual to XPS but I haven't found many examples where I can take an existing WPF page, with various controls like labels, listboxes, grids, etc and create that into an XPS document. Is there a code example out there that takes an entire XAML page and creates XPS?

推荐答案

这并不简单,这里的基本问题是XPS表示固定页面.现有的WPF页面不一定转换为文档中的页面.如果报表不适合页面,将如何拆分?此信息是必需的.

It's not trivial, the basic problem here is that XPS represent fixed pages. An existing WPF page does not necessarily translate to pages on a document. How will your report be split if it cannot fit the page? This information is needed.

您可以做的是将报告创建为FlowDocument(请参阅 http ://msdn.microsoft.com/en-us/library/aa970909.aspx ).这将为.NET框架提供有关如何对报告进行分页的足够信息,以便在执行此操作时:

What you can do is to create the report as a FlowDocument (see http://msdn.microsoft.com/en-us/library/aa970909.aspx). This will give the .NET framework enough info on how to paginate your report such that when you do this:

FlowDocument flowDocument;

// load, populate your flowDocument here

XpsDocument xpsDocument = new XpsDocument("filename.xps", FileAccess.ReadWrite);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);

有效. (代码从C#书中的Pro WPF中提取).

it works. (Code lifted from Pro WPF in C# Book).

这篇关于WPF-创建非Visual的XPS文档的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:12