Reputation: 93
I have created a custom document paginator that takes a datatable and prints exactly as I need. I would like to do a print preview. I have read all the posts on how to create a xps file in memory and then display it. I just can't get it to work. Here is my code. I am using a MVVM pattern. Please note the line of code _data.DocView=fds; This passes the data to my view model.
PrintDialog dialog = new PrintDialog();
dialog.ShowDialog();
StoreDataSetPaginator paginator = new StoreDataSetPaginator(dt, new Typeface("Calibri"), 8, 96 * 0.75,
new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
//this is commented out to attempt the print preview
// dialog.PrintDocument(paginator, "Print out");
MemoryStream ms = new MemoryStream();
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri DocumentUri = new Uri("pack://InMemoryDocument.xps");
PackageStore.AddPackage(DocumentUri, package);
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.NotCompressed,
DocumentUri.AbsoluteUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(paginator);
IDocumentPaginatorSource fds = xpsDocument.GetFixedDocumentSequence();
_data.DocView = fds;
PrintPreviewConduit prntv = new PrintPreviewConduit();
prntv.Show();
Now here is my view model:
private IDocumentPaginatorSource _docView;
public IDocumentPaginatorSource DocView
{
get { return _docView; }
set
{
_docView = value;
OnPropertyChanged("DocView");
}
}
And finally my XAML:
<Grid>
<DocumentViewer Name="docview" Document="{Binding DocView}"/>
</Grid>
I entered a break point in my ViewModel at "public IDocumentPaginatorSource DocView" and when I roll my mouse over it I get "System.Windows.Documents.FixedDocumentSequence. Not sure what i should be getting. I spent a good while now and any help would be greatly appreciated. Sys
Upvotes: 1
Views: 1075
Reputation: 93
Well I feel stupid. I did not set the new window's datacontext to my view model. Now everything works!!!!
PrintPreviewConduit prntv = new PrintPreviewConduit();
prntv.DataContext = _data;
_data.DocView = fds;
prntv.Show();
Upvotes: 1