En fait, après déconner avec des tas d'échantillons différents, qui sont tous incroyablement alambiqué et nécessitent l'utilisation des Ecrivains documents, conteneurs, files d'attentes et d'imprimer les billets, j'ai trouvé l'article Eric puits à propos Printing in WPF
Le code simplifié est un seulement 10 lignes longues
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;
}
Mon échantillon est assez simpliste, il ne comprend pas la page Dimensionnement et orientation qui contient un tout autre ensemble de questions qui ne fonctionnent pas comme on peut s'y attendre. Il ne contient pas non plus de fonctionnalité d'enregistrement, car MS semble avoir oublié d'inclure un bouton Enregistrer avec Document Viewer.
Enregistrer La fonctionnalité est relativement simple (et aussi de l'article Eric puits)
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();
}
}
Donc, la réponse est oui, vous pouvez prendre un Existant WPF (XAML) de contrôle, DataBind et le transformer en un XPS document - et ce n'est pas si difficile.
[http://msdn.microsoft.com/en-us/library/system.windows.xps.visualstoxpsdocument.aspx](http://msdn.microsoft.com/fr-fr/library/system.windows .xps.visualstoxpsdocument.aspx) –