I'm trying to save a FlowDocument as a XPS. I'm using .Net 4.0. The FlowDocument contains the following elements:
<Paragraph>
<InlineUIContainer BaselineAlignment="Center">
<Image Source="/MyNamespace;component/Images/logo.png"/>
</InlineUIContainer>
</Paragraph>
<Paragraph>
<Run Text="{Binding Path=TextBinding, FallbackValue='Oups, binding did not work'}"></Run>
</Paragraph>
<InlineUIContainer>
<DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False"
CanUserAddRows="False" RowHeaderWidth="0">
<DataGrid.Columns>
<DataGridTextColumn Header="Item Number"
Binding="{Binding Path=Id}" Width="100" />
<DataGridTextColumn Header="Description"
Binding="{Binding Path=ItemDescription}" Width="200" />
</DataGrid.Columns>
</DataGrid>
</InlineUIContainer>
Everything works just fine when I display that FlowDocument within my WPF app.
Now, I need to convert this into a XPS file on a server (I'd like to print in batch!).
So I'm using the following code:
flowDocument.DataContext = myViewModel;
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.SystemIdle,
new DispatcherOperationCallback(arg => null), null);
string xpsFilePath = String.Concat(@"c:\temp\", Guid.NewGuid(), ".xps");
using (FileStream outputFile = new FileStream(xpsFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (Package package = Package.Open(outputFile, FileMode.Create, FileAccess.ReadWrite))
using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Maximum))
{
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
rsm.SaveAsXaml(paginator);
rsm.Commit();
}
And guess what? 1) The image is missing 2) The DataGrid's empty, although the lines are drawn => Only the Run is displayed properly
I tried all sorts of combinations found on the web, but it just never comes out propertly. And any non automated solution is just so out of the question!!!
I can't wait for someone to rescue me!!! Thanks in advance