PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Fri May 16, 2025 4:43 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Feb 20, 2025 12:54 pm 
Offline

Joined: Thu Feb 20, 2025 10:28 am
Posts: 1
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);


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 24, 2025 8:25 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3128
Location: Cologne, Germany
Hi!

Do you get the exception with version 6.2.0 Preview 3?

If so, then please provide the PDF file for analysis.

See also:
http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 58 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group