Sco*_*ott 69
事实上,经过大量不同的样本,所有这些都令人难以置信的复杂,需要使用文档
编写器,容器,打印队列和打印票据,我发现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;
}
Run Code Online (Sandbox Code Playgroud)
我的示例相当简单,它不包括页面大小和方向,其中包含一组完全不同的问题.它也不包含任何保存功能,因为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();
}
}
Run Code Online (Sandbox Code Playgroud)
所以答案是肯定的,您可以使用现有的WPF(XAML)控件,对其进行数据绑定并将其转换为XPS文档 - 这并不是那么困难.
| 归档时间: |
|
| 查看次数: |
46904 次 |
| 最近记录: |