首页 新闻 会员 周边

xps包的文件内容读取

0
悬赏园豆:20 [待解决问题]

现有如下问题需要解决:

  当用户上传文件至服务器时,服务器上有一个文件夹监控器,当监控到有新添加的文件时,就把文件转化为XPS文件,现在需要读取xps文件的内容,但是不是在silverlight项目里面,所以不能使用application.getResourceStream(url)的方式。是否有另外的方式读取xps包中的内容。

JustYong的主页 JustYong | 初学一级 | 园豆:7
提问于:2012-06-15 11:20
< >
分享
所有回答(1)
0

来自Loading Xps from MemoryStream的参考代码:

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");
dudu | 园豆:31007 (高人七级) | 2012-06-15 13:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册