1

I'm in C# WPF.

I want to create a print function. First I generate a MemoryStream from an XmlDocument:

XmlDocument xmlDoc;
XslCompiledTransform _xsl; // Initialized before
/* creating Doc */

MemoryStream ms = new MemoryStream();
_xsl.Transform(xmlDoc, null, ms);
ms.Flush();
ms.Position = 0;

I can display the MemoryStream in a WebBrowser element using webBrowser.NavigateToStream(e.NewValue as Stream);. But now I want to display the MemoryStream before printing. I have a preview windows:

<Window Title="PrintView">    
    <Grid>          
        <DocumentViewer x:Name="printViewer"
                        Margin="10"
                        Document="{Binding DocumentView}"/>
    </Grid>
</Window>

The Binding element is:

FixedDocumentSequence _fixDoc = null;
public FixedDocumentSequence DocumentView
{
    get
    {
        return _fixDoc;
    }
    set
    {
        _fixDoc = value;
        OnPropertyChanged(nameof(DocumentView));
    }
}

But how can I create FixedDocumentSequence DocumentView from MemoryStream ms ?

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93

1 Answers1

1

You can achieve it by the following code:

PackageUriString: can just be anything you want.

private FixedDocumentSequence 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
  return document.GetFixedDocumentSequence();
}

Remember to keep the Package object in PackageStore until all operations complete on document.

  //Remove the package from store
  PackageStore.RemovePackage(packageUri);
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • I've try but instruction `Package package = Package.Open(xpsStream)` rise a FileFormatException "The file contains corrupted data". But displaying the same MemoryStream on WebBrowser works good. – A.Pissicat Aug 22 '16 at 13:55
  • What is this `bytes` when you say `XmlData`? – 123 456 789 0 Aug 22 '16 at 13:59
  • I don't understand your question. After creating my `MemoryStream ms`, I call your function and I have the error. To check my MemoryStream , I tried to create a `WebBrowser` instead of a `DocumentViewer`. In that case I do `webBrowser.NavigateToStream(ms)` and my windows display a correct page but non printable and without viewer. – A.Pissicat Aug 22 '16 at 14:37
  • From the documentation of the PackageStore: "XPS packages opened with an XpsDocument constructor are automatically added and removed from the PackageStore when the document is created and disposed. (You do not need to call AddPackage or RemovePackage separately for XPS packages opened with XpsDocument constructors.) " – Leonidas Oct 12 '18 at 04:52