1

i am trying to make help for my application. I have xps documents which i am loading to documentviewer. These files are embedded in resource file.

I am able to access these as bytearray. For example Properties.Resources.help_sudoku_methods_2 returns byte[]

However, documentviewer cant read it and requires fixeddocumentsequence. So i create memory stream from bytearray, then xpsdocument and then fixeddocumentsequence like this:

 private void loadDocument(byte[] sourceXPS)
        {
            MemoryStream ms = new MemoryStream(sourceXPS);
            const string memoryName = "memorystream://ms.xps";
            Uri memoryUri = new Uri(memoryName);
            try
            {
                PackageStore.RemovePackage(memoryUri);
            }
            catch (Exception)
            { }

            Package package = Package.Open(ms);


            PackageStore.AddPackage(memoryUri, package);

            XpsDocument xps = new XpsDocument(package, CompressionOption.SuperFast, memoryName);

            FixedDocumentSequence fixedDocumentSequence = xps.GetFixedDocumentSequence();
            doc.Document = fixedDocumentSequence;


        }

This is very unclean aproach and also doesnt work if there are images in files - instead of images in new documents displays images from first loaded doc.

Is there any cleaner way to load XPS from embedded resources to documentviewer? or do i need somethink like copy file from resources to application directory and load from here and not memorystream? Thank you.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Here is an example: http://geekswithblogs.net/shahed/archive/2007/09/22/115540.aspx – Morten Frederiksen Nov 13 '11 at 17:52
  • Actually i was using this article to construct my solution. It works fine when i have one file, but not well when i am loading one file after another. –  Nov 13 '11 at 18:06

1 Answers1

1

why dont you write file to system temp folder and then read from there.

    Stream ReadStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("file1.xps");
        string tempFile = Path.GetTempPath()+"file1.xps"; 
        FileStream WriteStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
        ReadStream.CopyTo(WriteStream);
        WriteStream.Close();
        ReadStream.Close();

        // Read tempFile INTO memory here and then

        File.Delete(tempFile);
ClearLogic
  • 3,616
  • 1
  • 23
  • 31
  • Thank you, this is similiar what i finally ended with. I created helf folder in my folder application and on first run or on files missing i write my embedded xps to this folder and then read it from here. –  Nov 14 '11 at 21:11