dotnet/wpf · error · XpsPackagingException

Package must contain an XPS PackagePart.

Error message

Package must contain an XPS PackagePart.

What it means

XpsFixedDocumentReaderWriter.AddPageToCache throws XpsPackagingException (ReachPackaging_PartNotFound) when GetPart returns no PackagePart for the given page URI, i.e. the package must contain an XPS PackagePart at that URI but none exists. This occurs while parsing an existing FixedDocument's page relationships into the page cache.

Solutions

  1. Fix or regenerate the source .xps file so every FixedDocument relationship targets an existing FixedPage part.
  2. Validate the package by re-saving it through the OPC/XPS API before opening.
  3. If the page is optional, remove the dangling relationship from the FixedDocument part before reading.
  4. Check part-name casing and relative-vs-absolute URI resolution in the relationship target.
Defensive patterns

Strategy: try-catch

Validate before calling

// before opening: verify relationship targets resolve
foreach (var rel in docPart.GetRelationshipsByType(XpsS0Markup.FixedPageRelationshipName))
    if (docPart.GetPart(rel.TargetUri) == null) throw new Exception("Missing part: " + rel.TargetUri);

Try / catch

try { ParsePages(); }
catch (XpsPackagingException ex) when (ex.Message.Contains("PackagePart")) { /* log broken URI; repair or reject the document */ }

Prevention

When it happens

Trigger: Opening an XPS package whose FixedDocument part has a /FixedPage relationship pointing to a URI with no corresponding part — a broken or hand-edited package.

Common situations: Corrupted or truncated .xps files; packages modified by third-party tools that dropped pages but left relationships; manually built OPC packages with mismatched URIs; part-name casing mismatches.

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/54fab95fcb1061e2. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentReaderWriter.cs:975

                            {
                                Uri relativeUri = new Uri(attribute, UriKind.Relative);
                                AddPageToCache(PackUriHelper.ResolvePartUri(Uri, relativeUri));
                            }
                        }
                    }
                }
            }
        }

        private
        IXpsFixedPageReader
        AddPageToCache( Uri pageUri )
        {
            PackagePart pagePart = CurrentXpsManager.GetPart(pageUri);

            if (pagePart == null)
            {
                 throw new XpsPackagingException(SR.ReachPackaging_PartNotFound);
            }

            if (!pagePart.ValidatedContentType().AreTypeAndSubTypeEqual(XpsS0Markup.FixedPageContentType))
            {
                throw new XpsPackagingException(SR.ReachPackaging_NotAFixedPage);
            }

            //
            // Create the reader/writer for the part
            //
            IXpsFixedPageReader pageReader = new XpsFixedPageReaderWriter(CurrentXpsManager, this, pagePart, null, _pageCache.Count+1);

            //
            // Cache the new reader/writer for later
            //
            _pageCache.Add(pageReader );

            return pageReader;

View on GitHub (pinned to 81131a70a4)