XpsDocument problem with DocumentViewer (NET 5.0)

Hi there

I am using your library for years now, but now I have problems showing the generated XpsDocument in DocumentViewer. I upgraded to .NET 5.0 and now I see this issue. Using 4.8 never showed this problem!

In short what i do:

var document = DocumentModel.Load(file)
// MailMerge docment
var xps = document.ConvertToXpsDocument(SaveOptions.XpsDefault)

// Document is bound to DocumentViewer.Document
Document = xps.GetFixedDocumentSequence(); 

The generated document is shown in the viewer, but after some time I get the following exception:

‘System.IO.IOException’ in System.IO.Compression.dll
Entries cannot be opened multiple times in Update mode.

If you want to have an example to reproduce, just let me know!

Regards Günter

Hi Günter,

This problem started when .NET Core added support for WPF, so since .NET Core 3.0.
The issue is reproduced only on .NET Core and only with XpsDocument objects that contain image data.

There are multiple GitHub issues for this on the .NET Runtime repo and I have created one as well.

For now, you can try using the following workaround:

var document = DocumentModel.Load(file)

var xpsStream = new MemoryStream();
document.Save(xpsStream, SaveOptions.XpsDefault);

var package = Package.Open(xpsStream, FileMode.Open, FileAccess.Read);
var uri = new Uri($"memorystream://{Guid.NewGuid():N}.xps");
PackageStore.AddPackage(uri, package);

var xps = new XpsDocument(package, CompressionOption.NotCompressed, uri.AbsoluteUri);

Document = xps.GetFixedDocumentSequence();

I hope this helps.

Regards,
Mario

Thx, I will give it a try!

In meantime I create a temporary xps file as workaround and delete it, when not needed anymore after preview and printing… That is working fine also!

Regards Günter

1 Like

Hi Günter,

Yes, saving to a temporary XPS file and then creating an XpsDocument object from it will also work.
Basically, any solution which creates a package in read-only mode (FileAccess.Read) should work.

Regards,
Mario

Hi Günter,

We have now added something similar to that workaround I mentioned above.
In other words, with the latest version, this issue will no longer occur.

So, please try again with the latest version from our BugFixes page or NuGet.
I hope this helps.

Regards,
Mario