Skip to content

Hybrid-reference PDFs (/XRefStm) are mis-read #388

Description

@vpben

Note: I'm not very familiar with the PDF format, so I have used an LLM to help track down this
problem. I've read everything it's produced and rewritten the text. This issue text is human-written,
the demo project is not - but I believe it to be correct.

Summary

PDFsharp ignores the /XRefStm key in a classic trailer dictionary, so it can't see any object that a hybrid-reference file stores in an object stream. Those objects are marked free (correctly) in the classic cross-reference table, so the document loads with holes in it and no error is raised at open time.

The failure occurs later, when a page whose object graph reaches one of the holes is imported into another document:

System.Collections.Generic.KeyNotFoundException: The given key '9 0' was not present in the dictionary.
   at PdfSharp.Pdf.Advanced.PdfImportedObjectTable.get_Item(PdfObjectID externalID)
   at PdfSharp.Pdf.PdfObject.FixUpObject(PdfImportedObjectTable iot, PdfDocument owner, PdfObject value)
   at PdfSharp.Pdf.PdfObject.ImportClosure(PdfImportedObjectTable importedObjectTable, PdfDocument owner, PdfObject externalObject)
   at PdfSharp.Pdf.PdfPages.CloneElement(PdfPage page, PdfPage importPage, String key, Boolean deepCopy)
   at PdfSharp.Pdf.PdfPages.ImportExternalPage(PdfPage importPage)
   at PdfSharp.Pdf.PdfPages.Insert(Int32 index, PdfPage page)
   at PdfSharp.Pdf.PdfPages.Add(PdfPage page)
   at PdfSharp.Pdf.PdfDocument.AddPage(PdfPage page)

I'm not permitted to share the original document, since it's an internal report. I've been able to create a pair of minimal example files showing the different behaviours: hybrid-xref.pdf shows the issue, plain-xref.pdf does not.

The above happens in both 6.2.4 and 7.0.0-preview-1.

Steps to reproduce

As requested, I've attached an IssueSubmissionTemplate modified to show the problem. There's a REPRODUCTION.md file in the zip file.

It builds the two example files as part of running, so this should show the problem:

cd src/PDFsharp/src/IssueSubmission
dotnet run -f net10.0

I've not run the GDI or WPF projects since I'm running on macOS.

Expected results

Both files behave the same. They describe the same single page, differing only in how the
cross-reference information is laid out.

Actual results

  • plain-xref.pdf - succeeds.
  • hybrid-xref.pdf - throws KeyNotFoundException: The given key '9 0' was not present in the dictionary.

Ideally it'd be good to have PDFsharp support the /XRefStm key, but if that's not possible it might be nicer to have the error surface at open time?

Other

I viewed/rendered both files with Ghostscript and macOS Preview and the files look identical and open correctly in both, so I believe the example files to be valid. The original (not shared) files were generated with Canva.

I'm happy to answer any questions if I've not covered anything you need.

Attachments

Issue.zip
plain-xref.pdf
hybrid-xref.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions