我有一个 FlowDocument,我想将其转换为 XPS 文档并将其附加到电子邮件中并一起发送。我正在使用此代码
public static MemoryStream FlowDocumentToXPS(FlowDocument flowDocument, int width, int height)
{
MemoryStream stream = new MemoryStream();
using (Package package = Package.Open(stream, 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;
paginator.PageSize = new System.Windows.Size(width, height);
rsm.SaveAsXaml(paginator);
rsm.Commit();
}
}
stream.Position = 0;
Console.WriteLine(stream.Length);
Console.WriteLine(stream.Position);
return stream;
}
然后我使用以下代码附加它:
Attachment xps = new Attachment(FlowDocumentToXPS(FD, 768, 676), "FileName.xps", "application/vnd.ms-xpsdocument");
其中 FD 是我要转换的 FlowDocument,我收到了 0.0KB 大小的 XPS 文件附件,并且无法使用 XPS 查看器打开,我在这里缺少什么?
编辑:工作的最终代码,请参阅评论
提前致谢