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:
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.
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:
When a user uploads an older PDF document, I get an "Unexpected EOF" error at this line:
The error message is as follows:
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.