如何在 WPF 应用程序中生成 PDF 和 RTF 报告? [英] How to approach production of PDF and RTF reports in a WPF application?

查看:65
本文介绍了如何在 WPF 应用程序中生成 PDF 和 RTF 报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在开发一个 WPF 应用程序,我需要为它实现 PDF 和 RTF 格式的报告功能.报告大多采用表格形式.

I am working on a WPF application that I need to implement reporting functionality for, in PDF and RTF formats. The reports are mostly tabular.

应用程序没有数据库.相反,它的数据取自本地 XML 文件,我从中创建了视图模型,这些视图模型又被传递到 UI 以呈现给用户.我需要将这些视图模型用于报告.

The application doesn't have a database. Instead its data is taken from local XML files from which I create View Models which are in turn passed to the UI for presentation to the user. I need to use these View Models for the reports.

问题

满足此要求的最佳方法是什么,以便我可以使用现有的视图模型生成 PDF 和 RTF 格式的报告?

What is the best way for me to approach this requirement, so that I can use my existing View Models to produce reports in PDF and RTF formats?

潜力

我在想,如果这是一个 Web 应用程序,我可以生成 HTML 格式的报告并使用第 3 方工具将其转换为 PDF 和 RTF.我以前做过这个,我知道它会起作用.不幸的是,无法保证用户将拥有互联网连接,因此我必须将报告生成保留在本地.

I was thinking if this was a web application I could generate a report in HTML and use 3rd party tools to convert it to PDF and RTF. I've done this before, and I know it would work. Unfortunately there's no guarantee that the user will have an internet connection so I have to keep the report generation local.

所以我想知道如何使用 XAML 来定义报告模板.这可能吗?我在 CodePlex 上看到了一个用于 Xaml FlowDocument 或 XPS to PDF Converter 的实用程序,但这适用于 FlowDocument,但我不不认为这是我需要的.我将生成的报告主要是表格形式.

So I'm wondering about using XAML to define the report template. Is this possible? I see a utility for Xaml FlowDocument or XPS to PDF Converter on CodePlex but this works with FlowDocument, but I don't think this is what I need. The reports I'll be generating are mostly tabular.

推荐答案

事实证明:

Xaml FlowDocument 或 XPS 到 PDF 转换器

似乎是解决方案.

我可以在 FlowDocument 中指定表,并指定一个 DataContext 将我的视图模型绑定到它,然后我可以使用内置功能将其转换为 PDF,或者使用类似于以下内容的内容将其转换为 RTF:

I can specify tables in the FlowDocument and and specify a DataContext to bind my View Models to it, and then I can either convert that to PDF using the in-built functionality, or convert it to RTF with something similar to the following:

// get FlowDocument object
var uri = new Uri("/Documents/SamplePDF.xaml", UriKind.Relative);

FlowDocument doc = App.LoadComponent(uri) as FlowDocument;


// create TextRange representation of the FlowDocument
var content = new TextRange(doc.ContentStart, doc.ContentEnd);

if (content.CanSave(DataFormats.Rtf))
{
    using (var stream = new FileStream(@"C:\Path\To\file.rtf", FileMode.OpenOrCreate))
    {
        // save it
        content.Save(stream, DataFormats.Rtf);
    }
}

这篇关于如何在 WPF 应用程序中生成 PDF 和 RTF 报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆