empira / PDFsharp

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

Error when accessing PdfDocument.AcroForm.Fields #213

Open eZprava opened 1 week ago

eZprava commented 1 week ago

I tried to load the attached file and iterate over pdf.AcroForm.Fields. At first I encountered an error:

No appropriate constructor found for type: PdfAcroFieldCollection at PdfSharp.Pdf.PdfDictionary.DictionaryElements.CreateArray(Type type, PdfArray oldArray)

So I added a constructor it was looking for: public PdfAcroFieldCollection(PdfDocument document): base(document){ }

Error "solved", but next one appeared: 'Object already in table.' at PdfSharp.Pdf.Advanced.PdfCrossReferenceTable.Add(PdfObject value)

I tried to change Add to ObjectTable[value.ObjectID]=value.ReferenceNotNull;, then there was no exception, but pdf.AcroForm.Fields were empty.

21a352e0-dd03-4855-a1e5-82fb3690493c.pdf

Steps to reproduce: dotnet new console -n PdfSharpBug cd PdfSharpBug dotnet add package PdfSharp code .

using System.Net;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.IO;

using (var ms = new MemoryStream(new WebClient().DownloadData("https://github.com/user-attachments/files/17873789/21a352e0-dd03-4855-a1e5-82fb3690493c.pdf")))
using (var pdf = PdfReader.Open(ms, PdfDocumentOpenMode.Import))
{
    foreach (PdfReference fieldReference in pdf.AcroForm.Fields)
    {
        Console.WriteLine(fieldReference.ToString());
    }
}

dotnet run

Observed result: Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. at PdfSharp.Pdf.PdfDictionary.DictionaryElements.CreateArray(Type type, PdfArray oldArray) at PdfSharp.Pdf.PdfDictionary.DictionaryElements.GetValue(String key, VCF options) at PdfSharp.Pdf.AcroForms.PdfAcroForm.get_Fields() at Program.

$(String[] args) in C:\git\PdfSharpBug\Program.cs:line 8

ThomasHoevel commented 4 days ago

Please consider using the Issue Submission Template: https://docs.pdfsharp.net/General/Issue-Reporting.html

eZprava commented 4 days ago

I added steps needed to reproduce the NullReferenceException, hope it helps.