How to convert FlowDocument to FixedDocument using FlowDocument's IDocumentPaginatorSource DocumentPaginator?
Asked Answered
S

1

9

I have:

DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator;

and:

FlowDocument document = new FlowDocument();
// create a flow document...

How do a convert the FlowDocument to a FixedDocument using the DocumentPaginator? I can't seem to find out how to do this. All sites I looked at use XpsWriter as in:

XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc); 
DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator;
writer.Write(dp);

I could write the XPS and then get back the FixedDocument but there must be a better way?

Selfopinionated answered 21/8, 2013 at 16:29 Comment(0)
E
9

No, there doesn't seem to be a better way. But you don't need to save it to disk:

var paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
var package = Package.Open(new MemoryStream(), FileMode.Create, FileAccess.ReadWrite);
var packUri = new Uri("pack://temp.xps");
PackageStore.RemovePackage(packUri);
PackageStore.AddPackage(packUri, package);
var xps = new XpsDocument(package, CompressionOption.NotCompressed, packUri.ToString());
XpsDocument.CreateXpsDocumentWriter(xps).Write(paginator);
FixedDocument doc = xps.GetFixedDocumentSequence().References[0].GetDocument(true);
Eatage answered 1/2, 2014 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.