转换WPF(XAML)控制到XPS文档 [英] Convert WPF (XAML) Control to XPS Document

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

问题描述

我可以利用现有的WPF(XAML)控制,数据绑定,并把它变成一个XPS文档,可以使用WPF XPS文档查看器显示和打印?
如果是这样,怎么样?
如果不是这样,我应该怎么做使用XPS / PDF /等在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控件,数据绑定它来获得有用的数据进入,然后使其打印和saveable为最终用户。理想的情况是创建文档将在内存中完成,不会打磁盘,除非用户明确保存文档。这是可行的?

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?

推荐答案

其实有不同样品的堆,所有这一切都是令人难以置信的令人费解,并要求使用文档作家,容器,打印队列和打印门票,我的乱搞后了解印在WPF中发现埃里克汇文章

简化code是一个只有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.

保存功能是相对简单的(也是来自埃里克洗涤槽的文章)

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天全站免登陆