posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

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
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Loading Xps from MemoryStream

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.

Print | posted on Saturday, September 22, 2007 4:54 PM |

Feedback

Gravatar

# re: Loading Xps from MemoryStream

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.
11/14/2007 11:56 PM | Sam
Gravatar

# re: Loading Xps from MemoryStream

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).
3/14/2008 2:03 AM | Will
Gravatar

# re: Loading Xps from MemoryStream

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?
5/14/2008 2:16 AM | VinnieP
Gravatar

# re: Loading Xps from MemoryStream

My problem is to show that document in a partially trusted application, since the GetFixedDocumentSequence is not allowed in the internet zone... any ideas?
6/18/2008 7:03 AM | Carlos de Luna Saenz
Gravatar

# re: Loading Xps from MemoryStream

Can u please let me know is tehre any posiblity to convert a XPsdocument to PDF
9/12/2008 12:20 PM | Jilani Khan
Gravatar

# re: Loading Xps from MemoryStream

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.
9/12/2008 3:55 PM | Shahed Khan

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 3 and type the answer here:

Powered by: