从 FlowDocument 创建 XPS 文档并即时附加它
我有一个 FlowDocument,我想将其转换为 XPS 文档并将其附加到电子邮件中并一起发送。我正在使用此代码
public static Stream 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))
{
xpsDoc.AddFixedDocumentSequence();
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();
}
return stream;
}
}
然后我使用此代码附加它:
Attachment xps = new Attachment(FlowDocumentToXPS(FD, 768, 676), "FileName.xps", "application/vnd.ms-xpsdocument");
其中 FD 是我想要转换的 FlowDocument ,我收到附加的 0.0KB 大小的 XPS 文件,并且无法使用 XPS 查看器打开它,我是这里不见了?
I have a FlowDocument that I want to convert to an XPS Document and attach it to an e-mail and send it all together. I'm using this code
public static Stream 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))
{
xpsDoc.AddFixedDocumentSequence();
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();
}
return stream;
}
}
Then I attach it using this code:
Attachment xps = new Attachment(FlowDocumentToXPS(FD, 768, 676), "FileName.xps", "application/vnd.ms-xpsdocument");
where FD is the FlowDocument I want to convert , I'm receiving 0.0KB size XPS file attached and it can't be open with the XPS Viewer , what I'm missing here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决,这是最终有效的代码:
然后我使用以下代码附加它:
其中 FD 是我想要转换的 FlowDocument。
Solved , this is the final code which worked:
Then I attach it using this code:
where FD is the FlowDocument I want to convert.