• 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.

  • 相关阅读:
    Gym 101466(完整)
    HDU 3639 Hawk-and-Chicken (强连通缩点+DFS)
    hdu3394--Railway(点的双连通分量)
    hdu2732 Leapin' Lizards 最大流+拆点
    hdu2609 How many 字典树+最小表示法
    hdu2594 Simpsons’ Hidden Talents LCS--扩展KMP
    hdu2509 Be the Winner 博弈
    hdu2461 Rectangles 线段树--扫描线
    hdu2389 Rain on your Parade 二分图匹配--HK算法
    hdu2328 Corporate Identity 扩展KMP
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/8010607.html
Copyright © 2020-2023  润新知