7

我有:

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 但必须有更好的方法吗?

4

1 回答 1

8

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

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);
于 2014-02-01T18:59:11.190 回答