如何使用FlowDocument的IDocumentPaginatorSource DocumentPaginator将FlowDocument转换为FixedDocument?

时间:2013-08-21 16:29:36

标签: c# .net flowdocument fixeddocument

我有:

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

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

如何使用DocumentPaginator将FlowDocument转换为FixedDocument?我似乎无法找到如何做到这一点。我查看的所有网站都使用XpsWriter:

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

我可以编写XPS然后返回FixedDocument但是必须有更好的方法吗?

1 个答案:

答案 0 :(得分:6)

不,似乎没有更好的方法。但是您不需要将其保存到磁盘:

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);