Error while saving to stream: The value of Seek/OffsetFromPosition parameter is outside of the permitted range

I use Aspose.Pdf (20.11) to append one pdf to another and then save them to a stream, but I keep getting the following error:

The value of Seek/OffsetFromPosition parameter is outside of the permitted range. The current value is: -32. The permitted range is: from 0 to 2,147,483,647.

I have tried various overloads, but all seem to run into this error.

The sample AL code I use for testing is as follows:

MemoryStream := MemoryStream.MemoryStream();
AsposePdfDocument.Save(MemoryStream, AsposePdfSaveFormat.Pdf);

The AsposePdfDocument holds the merged item. I later added a Seek(0, 0) for the memory stream to zero effect.

The sample PDFs I used were created by pasting Lorem Ipsum text into a Word document and exporting that to PDF; both documents were one page.

@PeterConijn

Are you exporting both Word Documents to PDF using MS Word or Aspose.Words? Please share the complete code snippet that you are using so that we can test the scenario in our environment and address it accordingly. Please also try to set the stream position to zero before initializing new Aspose.Pdf.Document:

ms.Seek(0, SeekOrigin.Begin);

I don’t know how much this helps you, since this is Dynamics AL and not pure C#, but here is the snippet I used for testing. The DotNet objects are reflections of your dll.

codeunit 50025 "DYS PDF Management"

{

    trigger OnRun()

    begin 

        MergeTwoPdfs();

    end;

    [Scope('OnPrem')]

    local procedure MergeTwoPdfs()

    var

        AsposePdfDocument1: DotNet AsposePdfDocument; //First document to load

        AsposePdfDocument2: DotNet AsposePdfDocument; //Second document to load

        AsposePdfDocument3: DotNet AsposePdfDocument; //Document to collect the two (loading #2 into #1 directly gave an error)

        AsposePdfPageCollection: DotNet AsposePdfPageCollection; //Page Collection

        AsposePdfPage: DotNet AsposePdfPage; //Page

        

    begin

        LoadLicense(); //Load the license

        //Load the two files
        if not UploadFile(AsposePdfDocument1) then

            exit;

        if not UploadFile(AsposePdfDocument2) then

            exit;

        
        //Merge the files
        AsposePdfDocument3 := AsposePdfDocument1;

        foreach AsposePdfPage in AsposePdfDocument2.Pages do

            AsposePdfDocument3.Pages.Add(AsposePdfPage);

        
        // Save the files
        SaveFile(AsposePdfDocument3);

    end;

    local procedure LoadLicense()  //Method to load the license - this works fine

    var

        ELCSetup: Record "ELC Setup";

        TempBlob: Codeunit "Temp Blob";

        WriteStream: OutStream;

    begin

        if not IsNull(AsposePdfLicense) then

            exit;        

        ELCSetup.Get();

        TempBlob.CreateOutStream(WriteStream);

        ELCSetup."Aspose License".ExportStream(WriteStream);

        AsposePdfLicense := AsposePdfLicense.License();

        AsposePdfLicense.SetLicense(WriteStream);

    end;

    [Scope('OnPrem')]

    local procedure UploadFile(var AsposePdfDocument: DotNet AsposePdfDocument): Boolean;

    var

        TempBlob: Codeunit "Temp Blob";

        ReadStream: InStream;

        WriteStream: OutStream;

        Filename: Text;

    begin

        if not UploadIntoStream('Upload File', '', 'PDF Files|*.pdf|All Files|*.*', Filename, ReadStream) then

            exit;

        TempBlob.CreateOutStream(WriteStream);

        CopyStream(WriteStream, ReadStream);

        AsposePdfDocument := AsposePdfDocument.Document(WriteStream); //Use the stream to generate the document

        exit(not IsNull(AsposePdfDocument));

    end;

    [Scope('OnPrem')]

    local procedure SaveFile(var AsposePdfDocument: DotNet AsposePdfDocument)

    var

        TempBlob: Codeunit "Temp Blob";

        WriteStream: OutStream;

        ReadStream: InStream;

        Filename: Text;

        MemoryStream: DotNet MemoryStream;  //Reflection of .NET memory stream, since OutStream does not have a Seek method

        AsposePdfDocument2: DotNet AsposePdfDocument;

        AsposePdfSaveFormat: DotNet AsposePdfSaveFormat;

    begin

        TempBlob.CreateOutStream(WriteStream);

        MemoryStream := MemoryStream.MemoryStream();

        MemoryStream.Seek(0, 0);

        AsposePdfDocument.Save(MemoryStream, AsposePdfSaveFormat.Pdf);  //<---- This is where the exception occurs

                

        MemoryStream.CopyTo(WriteStream);

        TempBlob.CreateInStream(ReadStream);

        DownloadFromStream(ReadStream, 'Save file', '', 'PDF Files|*.pdf|All Files|*.*)', Filename);

        MemoryStream.Dispose();

    end;

    var

        AsposePdfLicense: DotNet AsposePdfLicense;

}

@PeterConijn

The error does not seem related to Aspose.PDF. It looks like an error related to MemoryStream not being set properly. Could you please share if you are facing this error with every PDF file you are processing or with a specific one? Please try to share some sample PDFs for our reference so that we can test the scenario in our environment and address it accordingly.

It is with every PDF file. Like I said, I simply pasted some Lorem Ipsum dummy text in a Word document and exported that as a PDF file. I attached two files for your to test with. Thanks!!

Lorem ipsum dolor sit amet.pdf (47.9 KB)
Nam pretium turpis et arcu.pdf (47.7 KB)

@PeterConijn

We are testing the scenario and will get back to you soon.

@PeterConijn

In order to investigate the issue that you were facing, we have logged an investigation ticket as PDFNET-49677 in our issue tracking system. We will inform you as soon as it is resolved after investigating it in details. Please be patient and give us some time.

We apologize for the inconvenience.