Hi everyone,
I am using PdfReader from PdfSharp to read a Pdf-file and it generates an ArgumentOutOfRangeException error. The error seems to indicate that the file is missing or has a problem although threre is no problem with the file. Here is the error:
"System.ArgumentOutOfRangeException: value ('-1') must be a non-negative value. (Parameter 'value') Actual value was -1. at System.ArgumentOutOfRangeException.ThrowNegative[T](T value, String paramName) at System.IO.MemoryStream.set_Position(Int64 value) at PdfSharp.Pdf.IO.Lexer.set_Position(Int32 value) at PdfSharp.Pdf.IO.Parser.MoveToObject(PdfObjectID objectID) at PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjecStream) at PdfSharp.Pdf.IO.Parser.GetStreamLength(PdfDictionary dict) at PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjecStream) at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider) at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode) at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, PdfDocumentOpenMode openmode)
and this is the code part that generates the error: using PdfDoc.PdfDocument separator = PdfReader.Open(@"./Documents/Separator.pdf", PdfDocumentOpenMode.Import); pdf.AddPage(separator.Pages[0]);
if (!IsAdvancedPayment) { List<XImage> images = new();
foreach (var attachment in Attachments) { var data = attachment.Content; Log.Debug("attachment: {attachment}", attachment.Content);
var stream = new MemoryStream(data);
switch (attachment.MetaData.MimeType) { case "image/jpeg" or "image/png": { images.Add(XImage.FromStream(stream)); break; } case "application/pdf": { stream.Position = 0; // Reset stream position to beginning Log.Debug("stream: {stream}", stream.CanRead); using var attachmentDocument = PdfReader.Open(stream, PdfDocumentOpenMode.Import); foreach (var page in attachmentDocument.Pages) pdf.AddPage(page); break; } } }
the error occurs at this line: using var attachmentDocument = PdfReader.Open(stream, PdfDocumentOpenMode.Import);
|