ArgumentOutOfRangeException when trying to use PdfDocument.Load

Using GemBox.Pdf 17.1386, I’m receiving a PDF file in base64 format, then I convert the string using Convert.FromBase64String to turn into a byte array.
Then I turn it into a new MemoryStream, and then I try to call PdfDocument.Load(memoryStream). When i call the Load method, my application brings the following exception:

System.ArgumentOutOfRangeException: ‘Argument must be between 1 and 2 (inclusive). Arg_ParamName_Name
ArgumentOutOfRange_ActualValue’

I already tried to set the memoryStream position to 0, with no success.
The error occurs only with some pdf files. With other, there is no error. I really don’t know what else to do.

This is my code, changed for brevity:

public string GetSignature(string authorizationToken, string PdfBase64, UserModel userModel)
{
    GemBox.Pdf.ComponentInfo.SetLicense("FREE-LIMITED-KEY");
    GemBox.Document.ComponentInfo.SetLicense("FREE-LIMITED-KEY");
    byte[] bytes = Convert.FromBase64String(PdfBase64);

    var memoryStream = new MemoryStream(bytes);
    memoryStream.Seek(0, SeekOrigin.Begin);
    PdfDocument document2 = PdfDocument.Load(memoryStream);
}

I’d really appreciate any help. Thanks

Hi Alessandro,

Please send us your PDF file so that we can reproduce the issue and investigate it.

Regards,
Mario

1 Like

thanks for answering mario

Can I send the files here in this forum? What is the best way to send them to you?

I’ll put here a link to a google drive with the pdf that don’t work with my current solution.

Thanks in advance.

Hi Alessandro,

I’m afraid that your file is invalid, if you open it with Adobe and try to close it, you’ll notice that it asks you to save the changes. That is because it did a silent repair when reading your file.

Anyway, the problem is with the file’s beginning:
image

Please try changing the “%PDF-0.0” value to “%PDF-1.7”.
Does this solve your issue?

Regards,
Mario

1 Like

You’re absolutely right Mario. I opened the same pdf onto notepad++, changed the first line from %PDF-0.0 to %PDF-1.7 and tried again, with success this time.

Now I’ll have to look why my application is generating a PDF invalid like this. I’ll do some research.

Thanks for helping.

–edit

I’ve managed to change my application to create the pdf files with the “right” header (with %PDF-1.6 instead of %PDF-0.0). My problem was that I was using the telerik reporting library to generate what they call a ReportBook, where they append multiple pdfs into a single pdf (even if there’s one single pdf). Somehow this ReportBook logic was creating invalid pdfs. I fixed it by printing every file by each own.