How to approach the creation of PDF and RTF reports in a WPF application?

Background

I am working on a WPF application in which I need to implement reporting functions for PDF and RTF formats. Reports are mostly tabular.

There is no database in the application. Instead, its data is taken from local XML files from which I create View Models, which, in turn, are passed to the user interface for presentation to the user. I need to use these view models for reports.

Question

How would I better approach this requirement so that I can use existing view models to create reports in PDF and RTF formats?

Potentials

I thought that if this is a web application, I can create a report in HTML and use third-party tools to convert it to PDF and RTF. I have done this before and I know it will work. Unfortunately, there is no guarantee that the user will have an Internet connection, so I must support local report generation.

So I'm curious about using XAML to define a report template. Is it possible? I see a utility for Xaml FlowDocument or XPS to PDF Converter on CodePlex, but it works with FlowDocument, but I don’t think that this is what I need. The reports that I will generate are mainly tabular.

0
source share
1 answer

It turns out:

Xaml FlowDocument or XPS to PDF Converter

seems to be a solution.

I can specify tables in a FlowDocument and specify a DataContext to bind view models to it, and then I can either convert them to PDF using the built-in functions, or convert them 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); } } 
0
source

Source: https://habr.com/ru/post/1015119/


All Articles