dotnet/wpf · error · XpsPackagingException

Package must contain an XPS PackagePart.

Error message

Package must contain an XPS PackagePart.

What it means

XpsPackagingException thrown by XpsFixedDocumentSequenceReaderWriter.AddDocumentToCache when a part at the given document URI cannot be retrieved from the XPS package (GetPart returned null). It means the FixedDocumentSequence references a document part that does not physically exist in the package. The library throws it while parsing documents to fail fast instead of returning a null reader.

Solutions

  1. Verify the package contains the FixedDocument part referenced by the FixedDocumentSequence (open the .xps as a zip and check /Documents/1/*.fpage paths).
  2. Regenerate the XPS file from its original source application; do not hand-edit package contents.
  3. Check for truncation — compare file size or re-download/copy the file.
  4. If building packages yourself, ensure every document URI added to the sequence has a corresponding XpsFixedDocumentReaderWriter committed before closing the package.
  5. Wrap XPS parsing in try-catch for XpsPackagingException and treat the file as corrupt, prompting re-export.

Example fix

// before
XpsDocument doc = new XpsDocument(path, FileAccess.Read);
FixedDocumentSequenceReader seq = doc.FixedDocumentSequenceReader; // may throw downstream
// after
try
{
    XpsDocument doc = new XpsDocument(path, FileAccess.Read);
    var seq = doc.FixedDocumentSequenceReader;
    if (seq == null) throw new InvalidDataException("No FixedDocumentSequence in package.");
}
catch (XpsPackagingException ex)
{
    Console.WriteLine($"Corrupt XPS package: {ex.Message}");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Before reading: confirm the referenced document parts exist
using (Package p = Package.Open(path, FileMode.Open, FileAccess.Read))
{
    foreach (PackageRelationship rel in packagePart.GetRelationshipsBySelector("http://schemas.microsoft.com/xps/2005/06/relativefrom/fixeddocumentsequence"))
    {
        Uri resolved = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
        if (!p.PartExists(resolved)) throw new InvalidDataException($"Missing part: {resolved}");
    }
}

Type guard

bool IsValidXpsPackage(string path) =>
    File.Exists(path) && new FileInfo(path).Length > 0 &&
    ZipFile.OpenRead(path).Entries.Any(e => e.FullName.EndsWith(".fdoc", StringComparison.OrdinalIgnoreCase));

Try / catch

try
{
    var seq = xpsDocument.FixedDocumentSequenceReader;
}
catch (XpsPackagingException ex)
{
    // treat package as corrupt; fail with actionable message
    throw new InvalidDataException($"XPS package missing a referenced FixedDocument part: {ex.Message}", ex);
}

Prevention

When it happens

Trigger: Reading/consuming an XPS package whose FixedDocumentSequence contains a document URI that has no corresponding PackagePart — e.g. a truncated, hand-edited, or corrupted .xps/.oxps file, or a package where relationships point to a part that was never written.

Common situations: Processing XPS files generated by third-party tools that wrote incomplete packages; opening a partially downloaded or truncated XPS; manually rebuilding an XPS zip and dropping a FixedDocument part; .NET WPF printing pipelines consuming malformed documents.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5046f596cf1968a3. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentSequenceReaderWriter.cs:611

        EnsureThumbnail()
        {
            if( _thumbnail == null )
            {
                _thumbnail = CurrentXpsManager.EnsureThumbnail( this, _metroPart );
            }
        }

        private
        IXpsFixedDocumentReader
        AddDocumentToCache(Uri documentUri)
        {
            //
            // Retrieve the requested part from the package
            //
            PackagePart documentPart = CurrentXpsManager.GetPart(documentUri);
            if (documentPart == null)
            {
                 throw new XpsPackagingException(SR.ReachPackaging_PartNotFound);
            }

            //
            // If the part is not a fixed document then throw an exception
            //
            if (!documentPart.ValidatedContentType().AreTypeAndSubTypeEqual(XpsS0Markup.FixedDocumentContentType))
            {
                throw new XpsPackagingException(SR.ReachPackaging_NotAFixedDocument);
            }

            //
            // Create the reader/writer for the part
            //
            IXpsFixedDocumentReader  fixedDocument = new XpsFixedDocumentReaderWriter(CurrentXpsManager, null, documentPart, _documentCache.Count+1);

            //
            // Cache the new reader/writer for later
            //

View on GitHub (pinned to 81131a70a4)