ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.05k stars 234 forks source link

Unexpected EOF error while reading PDF using PdfSharpCore #373

Open Bassam-Rahat opened 1 year ago

Bassam-Rahat commented 1 year ago

I am working on a project where I am using PdfSharpCore to modify PDF documents. My code reads PDFs from a byte array, modifies certain elements of the document, and then writes the modified document back to a byte array.

Here is the method where I am facing an issue:

private byte[] ModifyPDFDocument(ContentModifyDTO content)
{
    byte[] fileData = content.FileData;
    using (MemoryStream outStream = new MemoryStream())
    {
        using (MemoryStream inStream = new MemoryStream())
        {
            inStream.Write(content.FileData, 0, content.FileData.Length);
            PdfDocument document = PdfReader.Open(inStream, PdfReadAccuracy.Moderate);

            string customProperty = "/uuid";
            if (document.Info.Elements.ContainsKey(customProperty))
            {
                document.Info.Elements.Remove(customProperty);
            }
            document.Info.Elements.Add(new KeyValuePair<string, PdfItem>(customProperty, new
            PdfString(content.DocumentId.ToString())));

            string customPropertyKey = "/customPropertyKey";
            string customPropertyMetadata = "customPropertyMetadata";
            if (document.Info.Elements.ContainsKey(customPropertyKey))
            {
                document.Info.Elements.Remove(customPropertyKey);
            }
            document.Info.Elements.Add(
            new KeyValuePair<string, PdfItem>(
                 customPropertyKey,
                 new PdfString(customPropertyMetadata)
             )
         );

            document.Save(outStream);
            fileData = outStream.ToArray();
        }
    }

    return fileData;
}

When a user uploads an older PDF document, I get an "Unexpected EOF" error at this line:

PdfDocument document = PdfReader.Open(inStream, PdfReadAccuracy.Moderate);

The error message is as follows:

{
"type": null,
"message": "Unexpected EOF",
"details": [{"target": "Void Fill()", "message": "Unexpected EOF"}]
}

However, most of the documents are read without any problems. I am looking for a solution that can allow PdfSharp to read all PDFs without any errors. Alternatively, if there's a way to validate the PDFs before reading them with PdfSharp to avoid this error, I would appreciate any suggestions.

Thank you in advance for your help.

undeologist commented 8 months ago

@Bassam-Rahat I solved the problem downgrading the versions to : IronPdf" Version="2023.12.6" PdfSharpCore" Version="1.2.20"