将 WPF (XAML) 控件转换为 XPS 文档

发布于 2024-07-12 06:20:06 字数 248 浏览 10 评论 0原文

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

基本上,我想采用现有的 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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-07-19 06:20:06

实际上,在弄乱了一堆不同的样本之后,所有这些样本都非常复杂,需要使用文档编写器、容器、打印队列和打印票证,我发现了 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;
}

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

保存功能相对简单(也来自 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();
 }
}

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

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;
}

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();
 }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文