C# 将 WPF (XAML) 控件转换为 XPS 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/502198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert WPF (XAML) Control to XPS Document
提问by Scott
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 (XAML) 控件,对其进行数据绑定并将其转换为可以使用 WPF XPS 文档查看器显示和打印的 XPS 文档吗?如果是这样,如何?如果没有,我应该如何使用 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?
基本上我想采用现有的 WPF 控件,对其进行数据绑定以获取有用的数据,然后使其可打印和可保存给最终用户。理想情况下,文档创建将在内存中完成,除非用户专门保存文档,否则不会访问磁盘。这可行吗?
采纳答案by Scott
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
实际上,在处理了大量不同的样本后,所有这些样本都非常复杂,并且需要使用文档
编写器、容器、打印队列和打印票证,我找到了 Eric Sinks 关于在 WPF 中打印的文章
简化代码只有 10 行长
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;
}
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.
我的示例相当简单,它不包括页面大小和方向,其中包含一组完全不同的问题,这些问题无法按您的预期工作。它也不包含任何保存功能,因为 MS 似乎忘记在文档查看器中包含保存按钮。
Save Functionality is relatively simple (and is also from Eric Sinks article)
Save Functionality 比较简单(也是来自 Eric Sinks 的文章)
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();
}
}
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 文档 - 这并不是那么困难。