Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009


I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes www.pageflakes.com.
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.


Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET

Shahed Khan blog

A common way of loading XpsDocument is to load it from file:

XpsDocument document = new XpsDocument(filename, FileAccess.Read, CompressionOption.NotCompressed);
FixedDocumentSequence fixedDocumentSequence = document.GetFixedDocumentSequence();
//To view in the DocViewer
docViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;


But if we need to Load the Xps from a Stream we can use the Package Class and do the following:

private void 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
  FixedDocumentSequence fixedDocumentSequence = document.GetFixedDocumentSequence();
  //To view in the DocViewer
  docViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
  //Remember to keep the Package object in PackageStore until all operations complete on document.
  //Remove the package from store
  PackageStore.RemovePackage(packageUri);
  doc.Close(); 
}

//Calling the above function from Client
//
//Get bytes[] from MemoryStream
//byte[] xpsBytes = someStream.ToArray();
//For demonstration I am reading bytes from a file
byte[] xpsBytes = File.ReadAllBytes(@"somexps.xps");
//
LoadXpsFromStream( xpsBytes, "somexps.xps");

Hope this helps.

Posted on Saturday, September 22, 2007 4:54 PM | Back to top


Comments on this post: Loading Xps from MemoryStream

# re: Loading Xps from MemoryStream
Requesting Gravatar...
There is just a small problem with this: it does only work if you load the XpsDocument into the MemoryStream from an URI (packageUriString).

If you happen to get the data, say, from an SQL query, you would not have this Uri, therefore you'd need to save the data into a file and reload it into a MemoryStream to show it.

This is kind of contraproductive. Usually I'd want to show an XpsDocument from a MemoryStream *because* I don't want to store it in a temp file. If I have to store it into a file to show it, the MemoryStream mumbo jumbo is moot.
Left by Sam on Nov 14, 2007 11:56 PM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
Actually, you can serialize the XPS document to a xaml string to store it in a database. You can then use a stringstream on the resulting xaml or maybe stream it directly from a sql query (never done that, but it might be possible).
Left by Will on Mar 14, 2008 2:03 AM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
I'm running into the same problem...What if I want to get the FixedDocumentSequence from an XPS doc created from a MemoryStream?

I'm using a 3rd-party component that exports to XPS, but all I really want is the collection of DocumentPageViews so I can do my custom presentation. My attempt was to export to XPS, get the DocumentPaginator from the FixedDocumentSequence, and use that to populate the DocumentPageViews, but it fails because the URI for the XPS doc is invalid.

What's the point of loading from a MemoryStream if you still need a URI?
Left by VinnieP on May 14, 2008 2:16 AM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
My problem is to show that document in a partially trusted application, since the GetFixedDocumentSequence is not allowed in the internet zone... any ideas?
Left by Carlos de Luna Saenz on Jun 18, 2008 7:03 AM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
Can u please let me know is tehre any posiblity to convert a XPsdocument to PDF
Left by Jilani Khan on Sep 12, 2008 12:20 PM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
You can look at http://www.siberix.com/ I know it can produce PDF and XPS from XAML type XML document. But I am not sure whether that will allow you to convert XPS to PDF.
Left by Shahed Khan on Sep 12, 2008 3:55 PM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
public static Visual CloneVisualState(Visual targetElement)
{
Visual retvalue = null;
string memoryStreamUri = "memorystream://printstream";
try
{
using (MemoryStream xpsStream = new MemoryStream())
{
using (Package package = Package.Open(xpsStream, FileMode.Create))
{
Uri packageUri = new Uri(memoryStreamUri);
PackageStore.AddPackage(packageUri, package);
XpsDocument doc = new XpsDocument(package, CompressionOption.Fast, memoryStreamUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(targetElement);
retvalue = doc.GetFixedDocumentSequence().DocumentPaginator.GetPage(0).Visual;
PackageStore.RemovePackage(packageUri);
doc.Close();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return retvalue;
}
Left by Paul on Apr 30, 2009 12:30 AM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
Thanks paul, that worked a charm
Left by Lewis on Aug 19, 2009 11:56 PM

# re: Loading Xps from MemoryStream
Requesting Gravatar...
Can have any third party control to generate the XPS of WPF form Using VB.net 2008 on runtime.
Left by Durgesh on Oct 01, 2009 8:16 PM

Your comment:
 (will show your gravatar)
 


Copyright © Shahed Khan | Powered by: GeeksWithBlogs.net | Join free