在 DocumentViewer 中显示 html MemoryStream
Display html MemoryStream in DocumentViewer
我在使用 C# WPF。
我想创建一个打印功能。首先,我从 XmlDocument 生成一个 MemoryStream:
XmlDocument xmlDoc;
XslCompiledTransform _xsl; // Initialized before
/* creating Doc */
MemoryStream ms = new MemoryStream();
_xsl.Transform(xmlDoc, null, ms);
ms.Flush();
ms.Position = 0;
我可以使用 webBrowser.NavigateToStream(e.NewValue as Stream);
在 WebBrowser 元素中显示 MemoryStream。
但现在我想在打印前显示 MemoryStream。我有预览 windows:
<Window Title="PrintView">
<Grid>
<DocumentViewer x:Name="printViewer"
Margin="10"
Document="{Binding DocumentView}"/>
</Grid>
</Window>
绑定元素是:
FixedDocumentSequence _fixDoc = null;
public FixedDocumentSequence DocumentView
{
get
{
return _fixDoc;
}
set
{
_fixDoc = value;
OnPropertyChanged(nameof(DocumentView));
}
}
但是如何从 MemoryStream ms
创建 FixedDocumentSequence DocumentView
呢?
您可以通过以下代码实现:
PackageUriString
: 可以随心所欲。
private FixedDocumentSequence LoadXpsFromStream(Byte[] xpsByte, string packageUriString)
{
MemoryStream xpsStream = new MemoryStream(xpsByte);
using (Package package = Package.Open(xpsStream))
//Remember to create URI for the package
Uri packageUri = new Uri(packageUriString);
//Need to add the Package to the PackageStore
PackageStore.AddPackage(packageUri, package);
//Create instance of XpsDocument
XpsDocument document = new XpsDocument(package, CompressionOptions.MaximuCompression, packageUriString);
//Do the operation on document here
//Here I am viewing the document in the DocViewer
return document.GetFixedDocumentSequence();
}
记得将 Package 对象保存在 PackageStore 中,直到完成对文档的所有操作。
//Remove the package from store
PackageStore.RemovePackage(packageUri);
我在使用 C# WPF。
我想创建一个打印功能。首先,我从 XmlDocument 生成一个 MemoryStream:
XmlDocument xmlDoc;
XslCompiledTransform _xsl; // Initialized before
/* creating Doc */
MemoryStream ms = new MemoryStream();
_xsl.Transform(xmlDoc, null, ms);
ms.Flush();
ms.Position = 0;
我可以使用 webBrowser.NavigateToStream(e.NewValue as Stream);
在 WebBrowser 元素中显示 MemoryStream。
但现在我想在打印前显示 MemoryStream。我有预览 windows:
<Window Title="PrintView">
<Grid>
<DocumentViewer x:Name="printViewer"
Margin="10"
Document="{Binding DocumentView}"/>
</Grid>
</Window>
绑定元素是:
FixedDocumentSequence _fixDoc = null;
public FixedDocumentSequence DocumentView
{
get
{
return _fixDoc;
}
set
{
_fixDoc = value;
OnPropertyChanged(nameof(DocumentView));
}
}
但是如何从 MemoryStream ms
创建 FixedDocumentSequence DocumentView
呢?
您可以通过以下代码实现:
PackageUriString
: 可以随心所欲。
private FixedDocumentSequence LoadXpsFromStream(Byte[] xpsByte, string packageUriString)
{
MemoryStream xpsStream = new MemoryStream(xpsByte);
using (Package package = Package.Open(xpsStream))
//Remember to create URI for the package
Uri packageUri = new Uri(packageUriString);
//Need to add the Package to the PackageStore
PackageStore.AddPackage(packageUri, package);
//Create instance of XpsDocument
XpsDocument document = new XpsDocument(package, CompressionOptions.MaximuCompression, packageUriString);
//Do the operation on document here
//Here I am viewing the document in the DocViewer
return document.GetFixedDocumentSequence();
}
记得将 Package 对象保存在 PackageStore 中,直到完成对文档的所有操作。
//Remove the package from store
PackageStore.RemovePackage(packageUri);