将 WPF (XAML) 控件转换为 XPS 文档 [英] Convert WPF (XAML) Control to XPS Document

查看:52
本文介绍了将 WPF (XAML) 控件转换为 XPS 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用现有的 WPF (XAML) 控件,对其进行数据绑定并将其转换为可以使用 WPF XPS 文档查看器显示和打印的 XPS 文档吗?如果是这样,如何?如果没有,我应该如何使用 XPS/PDF/etc 在 WPF 中进行报告"?

Can I take an Existing WPF (XAML) Control, databind it and turn it into an XPS document that can be displayed and printed using the WPF XPS Document Viewer? If so, how? If not, how should I be doing ‘reporting’ in WPF using XPS/PDF/etc?

基本上,我想采用现有的 WPF 控件,对其进行数据绑定以获取有用的数据,然后使其可打印和可保存给最终用户.理想情况下,文档创建将在内存中完成,除非用户专门保存文档,否则不会访问磁盘.这可行吗?

Basically I want to take an existing WPF control, databind it to get useful data into it and then make it printable and saveable for the end user. Ideally the document creation would be done in memory and wouldn’t hit the disk unless the user specifically saved the document. Is this feasible?

推荐答案

实际上,在弄乱了一堆堆不同的样本后,所有这些都非常复杂,需要使用文档编写器、容器、打印队列和打印票证,我发现 Eric Sinks 文章关于在 WPF 中打印
简化后的代码只有 10 行

Actually after messing around with heaps of different samples, all of which are incredibly convoluted and require the use of Document Writers, Containers, Print Queues and Print Tickets, I found Eric Sinks article about Printing in WPF
The simplified code is a mere 10 lines long

public void CreateMyWPFControlReport(MyWPFControlDataSource usefulData)
{
  //Set up the WPF Control to be printed
  MyWPFControl controlToPrint;
  controlToPrint = new MyWPFControl();
  controlToPrint.DataContext = usefulData;

  FixedDocument fixedDoc = new FixedDocument();
  PageContent pageContent = new PageContent();
  FixedPage fixedPage = new FixedPage();

  //Create first page of document
  fixedPage.Children.Add(controlToPrint);
  ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
  fixedDoc.Pages.Add(pageContent);
  //Create any other required pages here

  //View the document
  documentViewer1.Document = fixedDoc;
}

我的示例相当简单,它不包括页面大小和方向,其中包含一组完全不同的问题,这些问题无法按您的预期工作.它也不包含任何保存功能,因为 MS 似乎忘记在文档查看器中包含保存按钮.

My sample is fairly simplistic, it doesn't include Page Sizing and Orientation which contains a whole different set of issues that don't work as you would expect. Nor does it contain any save functionality as MS seem to have forgotten to include a Save button with the Document Viewer.

保存功能相对简单(同样来自 Eric Sinks 的文章)

Save Functionality is relatively simple (and is also from Eric Sinks article)

public void SaveCurrentDocument()
{
 // Configure save file dialog box
 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
 dlg.FileName = "MyReport"; // Default file name
 dlg.DefaultExt = ".xps"; // Default file extension
 dlg.Filter = "XPS Documents (.xps)|*.xps"; // Filter files by extension

 // Show save file dialog box
 Nullable<bool> result = dlg.ShowDialog();

 // Process save file dialog box results
 if (result == true)
 {
   // Save document
   string filename = dlg.FileName;

  FixedDocument doc = (FixedDocument)documentViewer1.Document;
  XpsDocument xpsd = new XpsDocument(filename, FileAccess.ReadWrite);
  System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
  xw.Write(doc);
  xpsd.Close();
 }
}

所以答案是肯定的,您可以使用现有的 WPF (XAML) 控件,对其进行数据绑定并将其转换为 XPS 文档 - 这并不难.

So the answer is Yes, you can take an Existing WPF (XAML) Control, databind it and turn it into an XPS document - and its not all that difficult.

这篇关于将 WPF (XAML) 控件转换为 XPS 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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