empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
527 stars 130 forks source link

System.InvalidOperationException thrown when calling PdfReader.Open #70

Open jeffz29 opened 10 months ago

jeffz29 commented 10 months ago

After updating to 6.1.0-preview-1, I am unable to open a pdf for modification.

When calling PdfReader.Open(path, PdfDocumentOpenMode.Modify), I get a System.InvalidOperationException thrown in the FinishChildReferences method. I believe it is because the method is enumerating through the dictionary.Elements, but then modifies a value in the Elements property.

`static void FinishChildReferences(PdfDictionary dictionary, PdfDocument document, HashSet finishedObjects) { foreach (var element in dictionary.Elements) { var item = element.Value;

            // For PdfReference: Update reference, if necessary, and continue with referred item.
            if (item is PdfReference iref)
            {
                if (FinishReference(iref, document, out var newIref, out var value))
                    dictionary.Elements[element.Key] = newIref;
                item = value;
            }

            // Get and update item's references.
            FinishItemReferences(item, document, finishedObjects);
        }
    }

`

Expected Behavior

Using Version 1.50.5147, PdfReader.Open loaded the pdf file successfully.

Actual Behavior

After updating to 6.1.0-preview-1, System.InvalidOperationException gets thrown.

Steps to Reproduce the Behavior

Call the following code: var doc = PdfReader.Open(sourcePath, PdfDocumentOpenMode.Modify); Pdf file to follow.

ThomasHoevel commented 10 months ago

PDF file still to follow.

bradleyuk-coba commented 10 months ago

Having the same problem... any news?

TH-Soft commented 10 months ago

Having the same problem... any news?

Still waiting for the PDF file.

Eraclys commented 10 months ago

Hi,

I am having the same issue.

Dot net Framework 4.8 does not support modifying dictionary entries while being enumerated. This can be reproduced by running the following:

[Fact]
public void Test()
{
    var dict = new Dictionary<string, string>
    {
        {"1", "1"},
        {"2", "2"}
    };

    foreach (var element in dict)
    {
        dict[element.Key] = "3";
    }
}

This will fail on 4.8 but will work on net 6.

A quick fix is to just do a .Tolist: foreach (var element in dictionary.Elements.ToList())

I hope that helps

bradleyuk-coba commented 10 months ago

Any PDF with content would probably cause the error (works fine in net6 but fails in net48) but here's an example:

Issue70Example.pdf

TH-Soft commented 10 months ago

We fixed this issue at two code locations with 6.1.0 Preview 2 (coming sooner or later). We check for other affected code spots.

bradleyuk-coba commented 10 months ago

Great... When do you think 6.1.0 Preview 2 will be available on nuget? 🤞

TH-Soft commented 10 months ago

Great... When do you think 6.1.0 Preview 2 will be available on nuget? 🤞

Sooner or later. No ETA set yet.