none
Partialy mixed up displayed content of DocumentViewer after loading different XPS file with the same name as previous RRS feed

  • Question

  • Hi everybody!

    If DocumentViewer (WPF Control) displays XPS file first time after the launch of my app, everything is OK!
    But if DV loaded another file with the SAME NAME as the previous file, the displayed content is not correct completely. Some pictures don't match with original but they come from a previously loaded file. I spend a lot of time to solve this problem, but I was not succeeded.

    It looks like a DV or XPSDocument object has a "cache". And I need to erase it before loading of next XPS file.

    Here my code for loading of XPS file in my app:

    public static void nacitajXPS(String pXpsPath, DocumentViewer pDV)
    {
        FileInfo XPS_FI = new FileInfo(pXpsPath);
        if (XPS_FI.Exists && XPS_FI.Length > 0)
        {
            pDV.Document = null;
            using (XpsDocument pXpsDoc = new XpsDocument(XPS_FI.FullName, FileAccess.Read, CompressionOption.NotCompressed))
            {
    
                pDV.Document = pXpsDoc.GetFixedDocumentSequence();
    
                pDV.FitToHeight();
    
                var theXpsPackage = System.IO.Packaging.PackageStore.GetPackage(pXpsDoc.Uri);
    
                if (theXpsPackage != null)
                {
                    theXpsPackage.Close();
                    System.IO.Packaging.PackageStore.RemovePackage(pXpsDoc.Uri);
                }
    
                theXpsPackage = null;
                pXpsDoc.Close();
                CESTY_DISK.PRAC_INSTRUKCIA_XPS.Delete();
            }
        }
    }


    Thursday, October 3, 2019 6:38 PM

All replies

  • Hi durcer,

    Thank you for posting here.

    Since your question is related to WPF, I will move it to WPF forum to get professional support.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 4, 2019 7:47 AM
  • Hi,

    You can try to load the xps file using a Stream,

     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
                    documentViewer1.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);
                    document.Close();
                }
                   
            }

    http://geekswithblogs.net/shahed/archive/2007/09/22/115540.aspx

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 7, 2019 5:58 AM
    Moderator
  • Thanks Alex for your reply, but it does not help me.

    The resulted behaviour of DocumentViewer in my app is the same as the case with my original code. :0(

    Monday, October 7, 2019 12:20 PM
  • Hi,

    When the file you load the second time has the same name as the file you first loaded, is the displayed content is not correct completely?If the name is different, this issue will not appear?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 8, 2019 2:10 PM
    Moderator
  • No completely! Only some pictures are incorrect.

    I have not tried to change names of files. My app is designed to have every downloaded file same name. (PRACOVNA_INSTRUKCIA.xps). The app renames files right after downloading. 

    I try to insert some sample screens, but system don't allowed me to do it. :0( 

    "Body text cannot contain images or links until we are able to verify your account."

    Is there anther possibility to load a document to DocumentViewer than: 

    FixedDocumentSequence fixedDocumentSequence = document.GetFixedDocumentSequence();
    pDV.Document = fixedDocumentSequence as IDocumentPaginatorSource;

    ?????????



    • Edited by duracer Wednesday, October 9, 2019 8:20 AM
    Tuesday, October 8, 2019 6:46 PM
  • My code doesn't seem to have your issue.

    private void button1_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "XPS 文档(*.xps)|*.xps";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    XpsDocument doc = new XpsDocument(dialog.FileName, System.IO.FileAccess.Read);
                    documentViewer1.Document = doc.GetFixedDocumentSequence(); 
                }
            }

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 9, 2019 2:42 PM
    Moderator
  • I try your code. It works OK when single files are in different locations or have different names.

    Problem is it's not possible to delete no files loaded after launch of app with this code. Even though files is not already loaded. Therefore you can not simulate working of my app by this way.

    In my app I need to delete old file and replace it by file with new content but with the same name. And this one without close app.




    • Edited by duracer Friday, October 11, 2019 12:18 PM
    Friday, October 11, 2019 12:10 PM
  • Hi,

    I don't know if your problem has been solved?I can't reproduce your problem.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, October 20, 2019 1:16 PM
    Moderator
  • Hi Alex,

    I have solved my problem by ensuring every file has different name.

    Best Regards,

    duracer

    Saturday, October 26, 2019 11:37 AM
  • Hi,

    Ok, please mark your reply as  answer.It will be beneficial to other community.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, October 28, 2019 2:01 AM
    Moderator