Retaining anchors when placing a flow document via pagination
I am trying to create a simple reporting and printing system in WPF in .NET 4 and after countless hours trawling SO and various online tutorials I have the following (simplified) setup which should accept a streaming document containing a report template add the data source to as a datacontext, put it through pagination and get something display / printable.
The report is laid out in a streaming document in a separate content file (DefaultReport.xaml):
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Paragraph FontFamily="Arial">
<Run Text="1"/>
<Run Text="{Binding Path=MyText}"/>
<Run Text="3"/>
</Paragraph>
</FlowDocument>
Datacontext is a simple object:
private class MyDataContext {
public string MyText { get; set; }
}
Loads and stacks like this:
using (var stream = File.OpenRead("DefaultReport.xaml")) {
FlowDocument document = (FlowDocument)XamlReader.Load(stream);
document.DataContext = new MyDataContext { MyText = "2" };
flowReader.Document = document;
XpsDocument xpsDoc = LoadAsXPS(((IDocumentPaginatorSource)document).DocumentPaginator);
fixedReader.Document = xpsDoc.GetFixedDocumentSequence();
xpsDoc.Close();
}
LoadAsXPS turns a flowdocument into an XpsDocument like this:
public XpsDocument LoadAsXPS(DocumentPaginator paginator) {
MemoryStream stream = new MemoryStream();
Package docPackage = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);
Uri uri = new Uri(@"memorystream://myXps.xps");
PackageStore.AddPackage(uri, docPackage);
XpsDocument xpsDoc = new XpsDocument(docPackage);
xpsDoc.Uri = uri;
XpsDocument.CreateXpsDocumentWriter(xpsDoc).Write(paginator);
return xpsDoc;
}
flowReader and fixedReader output the results of the whole operation and are defined in xaml as
<FlowDocumentReader Name="flowReader" />
<DocumentViewer Margin="0,10,0,0" Name="fixedReader" />
The end result is this:
The top half is the FlowDocumentReader which I use for debugging. Everything is fine here. The bottom half is the DocumentViewer containing my paginated document, which is what I need to get it working properly.
How to accurately store anchor data through the pagination process?
source to share