WPF - 创建非 Visual XPS 文档的示例

发布于 2024-08-14 11:49:08 字数 184 浏览 9 评论 0 原文

我正在寻找不涉及 RDLC/SSRS 的报告/打印解决方案。我想使用 DocumentViewer,我知道它支持 XPS。我发现了很多使用 Visual to 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?

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

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

发布评论

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

评论(2

苍暮颜 2024-08-21 11:49:08

这并不简单,这里的基本问题是 XPS 代表固定页面。现有的 WPF 页面不一定会转换为文档上的页面。如果您的报告无法容纳页面,将如何拆分?需要此信息。

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

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);

它可以工作。 (代码取自 Pro WPF in C# Book)。

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.

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);

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

绮筵 2024-08-21 11:49:08

通常,您的 WPF 页面有一个根 UI 元素,例如 Grid。由于网格是一种特定类型的视觉对象(请参阅“继承层次结构”部分@http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx 了解更多详细信息),您只需像其他一样编写根 Grid 元素XPS 中的视觉效果。然后所有嵌入的控件将自动写入XPS文档中。

Usually your WPF page has a root UI element, say Grid. As Grid is a specific type of Visual(please vide "Inheritance Hierarchy" part @http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx for more details), you just need to write that root Grid element like other visuals into XPS. And then all embedded controls will be automatically written into XPS document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文