Free Support Forum - aspose.com

Append method using Memory Stream

Hi,

I am new to using Aspose.Pdf.Kit. I am trying to Append multiple PDF documents togeter. I can do this by specifying the path of the documents or by using FileStream to load the documents and then call the Append method. I am trying to do the same using MemoryStream instead of FileStream as all my PDF files are stored binary in the database.

However when I use MemoryStream instead of File Stream I am getting error:

"Invalid pdf format:pdf head signature is not found!: v3.7.0.0 - "

In the same example if I am using FileStream I have no issues.

Here is my test code (here I just converted FileStream to MemoryStream for testing but when using in application I will get memory stream from database)


//Initialize the string variables storing paths of PDF files

string inFile1 = "\\input1.pdf";

string inFile2 = "\\input2.pdf";


//Creating stream objects holding the PDF files in Open Mode

FileStream inStream1 = new FileStream(inFile1, FileMode.Open);

FileStream inStream2 = new FileStream(inFile2, FileMode.Open);



MemoryStream storeStream1 = new MemoryStream();
MemoryStream storeStream2 = new MemoryStream();
MemoryStream storeStream3 = new MemoryStream();

storeStream1.SetLength(inStream1.Length);
inStream1.Read(storeStream1.GetBuffer(), 0, (int)inStream1.Length);

storeStream2.SetLength(inStream2.Length);
inStream2.Read(storeStream1.GetBuffer(), 0, (int)inStream2.Length);

//Instantiating PdfFileEditor object

PdfFileEditor editor = new PdfFileEditor();

//Calling Append method and providing five arguments

editor.Append(storeStream1, storeStream2, 1, 1, storeStream3);


This message was posted using Aspose.Live 2 Forum

Hi Sandra,

Have you tried the latest version of the component - Aspose.Pdf.Kit for .NET 3.8. Please try this version at your end. If you still find the problem, then please share the problematic PDF files with us, so we could test the issue at our end.

We’re sorry for the inconvenience.
Regards,

Hi,

I was using version 3.7 but now I have tried 3.8 with same results. I am still getting the same error. If I user PDF file that has no form fileds defined in it, it works fine but when I use PDF file that has form fields defined I get this error.
I am attaching PDF files I have issue with.

Can you see if you can append those two together using Append mehod and MemoryStream (as opposed to calling file or using FileStream).

Thanks
Sandra

Hi Sandra,

I have tested the files at my end using version 3.8 and noticed the same problem. I have logged the issue as PDFKITNET-11797 in our issue tracking system. Our team will be looking into the matter and you’ll be updated via this forum once the issue is resolved.

We’re sorry for the inconvenience.
Regards,

Hi Sandra,

We have further investigated this issue and found that there is a problem with your code. In the following lines of code, you’re trying to fill the second memory stream with the PDF file data, however you filled the first memory stream again, which leaves the second memory stream empty and that’s why the exception:


storeStream2.SetLength(inStream2.Length);

inStream2.Read(storeStream1.GetBuffer(), 0, (int)inStream2.Length);

Please change your code as below:

storeStream2.SetLength(inStream2.Length);

inStream2.Read(storeStream2.GetBuffer(), 0, (int)inStream2.Length);

I hope this helps. If you still find any issues or have some more questions, please do let us know.
Regards,

Thank you for your help.
I have it working in this instance right now.
However I am still having problems when I take file directly from the database where it is stored in the byte array.
If I take the byte array from database, save it to disk, open version from disk, load it to memory stream and then pass it to Append method that it works.

However if I want to skip the step of saving file to disk, so I take my byte array from database, load it to memory stream and then pass it to Append method I get following error:

{"Invalid pdf format:pdf head signature is not found!: v3.8.0.1"}

So in code following works:

public static void Test1(Byte[] b1, Byte[] b2)

{

MemoryStream ms1 = new MemoryStream(b1);

MemoryStream ms2 = new MemoryStream(b2);

SaveMemoryStream(ms1, @"C:\1.pdf");

SaveMemoryStream(ms2, @"C:\2.pdf");

string inFile1 = @"C:\1.pdf";

string inFile2 = @"C:\2.pdf";

string outFile = @"C:\3.pdf";

FileStream inStream1 = new FileStream(inFile1, FileMode.Open);

FileStream inStream2 = new FileStream(inFile2, FileMode.Open);

MemoryStream storeStream1 = new MemoryStream();

MemoryStream storeStream2 = new MemoryStream();

MemoryStream storeStream3 = new MemoryStream();

storeStream1.SetLength(inStream1.Length);

inStream1.Read(storeStream1.GetBuffer(), 0, (int)inStream1.Length);

storeStream2.SetLength(inStream2.Length);

inStream2.Read(storeStream2.GetBuffer(), 0, (int)inStream2.Length);

storeStream1.Position = inStream1.Length;

storeStream2.Position = inStream2.Length;

PdfFileEditor editor = new PdfFileEditor();

storeStream3.Capacity = storeStream1.Capacity + storeStream2.Capacity;

editor.Append(storeStream1, storeStream2, 1, 1, storeStream3);

SaveMemoryStream(storeStream3, outFile);

}

And following does NOT work:

public static void Test2(Byte[] b1, Byte[] b2)

{

string outFile = @"C:\3.pdf";

MemoryStream storeStream1 = new MemoryStream(b1);

MemoryStream storeStream2 = new MemoryStream(b2);

MemoryStream storeStream3 = new MemoryStream();

storeStream1.Position = storeStream1.Length;

storeStream2.Position = storeStream2.Length;

PdfFileEditor editor = new PdfFileEditor();

storeStream3.Capacity = storeStream1.Capacity + storeStream2.Capacity;

editor.Append(storeStream1, storeStream2, 1, 1, storeStream3);

SaveMemoryStream(storeStream3, outFile);

}

Any idea on what is wrong that would cause that error?

Thanks

Sandra

Hi Sandra,

Can you please try using the following statements?

storeStream1.Seek(0, SeekOrigin.Begin);
storeStream2.Seek(0, SeekOrigin.Begin);

Instead of:

storeStream1.Position = storeStream1.Length;
storeStream2.Position = storeStream1.Length;

I hope this will help. If you still find any issue or have some other questions, please do let us know.
Regards,


Hi,

If I do that, I get "Object reference not set to an instance of object" error

Hi Sandra,

Please share a small sample project containing the code snippet, and database with the sample data to reproduce the issue. In fact, we’re unable to reproduce this problem at our end, however In order to understand and then resolve this issue we need to create the scenario at our end and reproduce the issue.

We appreciate your patience and cooperation.
Regards,