実際、さまざまなサンプルのヒープをいじった後、それらはすべて非常に複雑で、ドキュメントライター、コンテナー、印刷キュー、印刷チケットを使用する必要があります。WPFでの印刷に関するEricSinksの記事を見つけました
。簡略化されたコードはわずか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ドキュメントに変換できます。それほど難しいことではありません。