Reputation: 1671
I'm using the Spire library to convert a DocX file to an XPS file so that I can display a preview of the document in my Windows Desktop (WPF, C#) application.
The conversion is fine - and I can save the resulting XPS file to a temporary file location. I can then open the XPS file with Packaging.XpsDocument
and use GetFixedDocumentSequence
to display the XPS document in a DocumentViewer control - all pretty simple so far.
To speed the process, I'd really like to save the XPS to a MemoryStream and just load the XPS from there. I have attempted the following:
FileStream fileStream = File.OpenRead(FileName);
MemoryStream msXps = new MemoryStream();
Spire.Doc.Document doc = new Spire.Doc.Document(fileStream, Spire.Doc.FileFormat.Docx);
doc.SaveToStream(msXps, Spire.Doc.FileFormat.XPS);
var package = System.IO.Packaging.Package.Open(msXps, FileMode.Open, FileAccess.Read);
System.Windows.Xps.Packaging.XpsDocument xpsDoc =
new System.Windows.Xps.Packaging.XpsDocument(package);
return xpsDoc.GetFixedDocumentSequence();
I have copied and pasted what I have as a test function right now - I have removed using statements for the purposes of this. My example compiles and I get the following error:
System.Windows.Xps.XpsPackagingException: 'ReachPackaging_PackageUriNull'
It appears that I can also pass the compression type to the XpsDocument ctor, and I can pass a Uri - but in this case there is no Uri - the Xps document is in memory and is not backed by any physical store.
Of course, I can keep using a temporary file, but it feels like there should be no need to touch the disk for this conversion.
Upvotes: 1
Views: 1109
Reputation: 812
You can use the .NET XpsDocument and PdfSharp and add PackageUri to the package
using (MemoryStream memoryStream = new MemoryStream())
{
System.IO.Packaging.Package package = System.IO.Packaging.Package.Open(memoryStream, FileMode.OpenOrCreate);
// ...
var packageUri = new Uri("memorystream://myXps.xps");
PackageStore.AddPackage(packageUri, package);
XpsDocument doc = new XpsDocument(package, CompressionOption.SuperFast, packageUri.AbsoluteUri);
XpsConverter.Convert(doc, filePath, 0);
package.Close();
}
Upvotes: 0