1

I am using the WPF DocumentViewer to preview an XPS document in my application. The code is actually converting from PDF to XPF actually. When I scroll down during preview, it behaves as expected. However, the moment I reach the point where the second page must appear I get the error:

(Dispatcher): [System.NotImplementedException: This method is not implemented by this class. at System.Net.WebRequest.BeginGetResponse(AsyncCallback callback, Object state) at System.IO.Packaging.PackWebRequest.GetResponse() at MS.Internal.WpfWebRequestHelper.GetResponse(WebRequest request) at MS.Internal.WpfWebRequestHelper.GetResponseStream(WebRequest request, ContentType& contentType) at System.Windows.Documents.PageContent._LoadPageImpl(Uri baseUri, Uri uriToLoad, FixedPage& fixedPage, Stream& pageStream) at System.Windows.Documents.PageContentAsyncResult.Dispatch(Object arg) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)].

I don't understand the WebRequest because I am working in a closed network where there is no Internet allowed. I do not need it for the preview.

My code is below. Some code was written from information coming from this post. I was originally getting an error described in that post which seems to be fixed, but now I am getting the above error and cannot figure out why. The error says that a "method is not implemented" but do not know which method it is talking about. It looks like it is making an asynchronous call when I scroll. Can someone explain what it is doing and how to proceed?

    public void PreviewDocument()
    {
        m_documentService = GcsIocContainer.Instance.Container.Resolve<IDocumentService>();
        var xpsDocument = m_documentService.ConvertPdfToXps(Convert.FromBase64String(PdfBase64String));

        FixedDocumentSequence fixedDocumentSequence = GetFixedDocumentSequence(xpsDocument);
        m_nativeDocumentViewer.Document = fixedDocumentSequence;
    }

    public class MemoryStreamUri : IWebRequestCreate
    {

        public WebRequest Create(Uri uri)
        {
            return new MemoryStreamRequest(uri);
        }
    }

    public class MemoryStreamRequest : WebRequest
    {
        private Uri _uri;

        public MemoryStreamRequest(Uri uri)
        {
            _uri = uri;
        }

        public override Uri RequestUri
        {
            get { return _uri; }
        }

        public override int Timeout
        {
            get { return System.Threading.Timeout.Infinite; }

        }
    }

    private FixedDocumentSequence GetFixedDocumentSequence(byte[] xpsBytes)
    {
        Uri packageUri;
        XpsDocument xpsDocument = null;

        var memURi = new MemoryStreamUri();
        WebRequest.RegisterPrefix("memorystream", memURi);

        using (MemoryStream xpsStream = new MemoryStream(xpsBytes))
        {
            using (Package package = Package.Open(xpsStream))
            {
                packageUri = new Uri(@"memorystream://" + Path.GetFileName(Guid.NewGuid().ToString()) + ".xps");

                try
                {
                    PackageStore.AddPackage(packageUri, package);
                    xpsDocument = new XpsDocument(package, CompressionOption.Maximum, packageUri.AbsoluteUri);

                    return xpsDocument.GetFixedDocumentSequence();
                }
                catch (Exception ex)
                {
                    var msg = ex.Message;
                    return null;
                }
                finally
                {
                    if (xpsDocument != null)
                    {
                        xpsDocument.Close();
                    }
                    PackageStore.RemovePackage(packageUri);
                }
            }
        }
    }
Ray
  • 4,679
  • 10
  • 46
  • 92

0 Answers0