Cannot Concatenate PDF files (as streams) to a MemoryStream (instead of FileStream)

I am trying to concatenate a stream array (of streamed PDFs) in a MemoryStream. I cannot seem to get it to work when using a MemoryStream as the outStream in the call...

PdfFileEditor.Concatenate(Stream[] inStream, Stream outStream)

When I use this code...

List<MemoryStream> masterList

....

PdfFileEditor pdfEditor = new PdfFileEditor();

MemoryStream ms = new MemoryStream();

pdfEditor.Concatenate(masterList.ToArray(), ms);

The code breaks on the last line - the error is that ms has been disposed.

I can get it to work if I use a FileStream for outStream (as per the examples in the Aspose.Pdf.Kit), but I want it to work with a MemoryStream as outStream.....Can you help me out?

Regards,

Kyle Peters

Hi Kyle,

Please download a hot fix for this issue from this link. Please note that with this hot fix, you’ll also have to set the position of the input memory streams to 0 i.e. inStream.Position = 0;.

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

I am still having problems after getting the hotfix, and trying to set the MemoryStream.Position=0. I don't understand what could be the problem now. I have two solutions that are getting different problem results with the exact same code. One solution is my 'real solution' that I can't send you. The other one concatenates, but is corrupt...I am attaching that solution.

Here is the code I am using....

//This is a MemoryStream array - examinationreveals that all members have a Length, and Position=0

List<MemoryStream> msList = new List<MemoryStream>();

MemoryStream ms = new MemoryStream();

PdfFileEditor pdfEditor = new PdfFileEditor();

ms.Position = 0;

pdfEditor.Concatenate(msList.ToArray(), ms);

//Now examination in one solution has ms.Length=0, and the other solution as a Length>0!

fsOut.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);

fsOut.Close();

//Examine the outTest.pdf from the attached solution, and it is corrupt. My other solution just has outTest.pdf as 0 bytes!

Here's a complete method that you can toggle the attempted concatenation from MemoryStream or to On-Disk. Both are corrupting all but the fist PDF...and tried many different PDF files in my testing.

static void ConcatenateMultipleInMemory()

{

List<MemoryStream> msList = new List<MemoryStream>();

PdfFileEditor pdfEditor = new PdfFileEditor();

String[] fileEntries = Directory.GetFiles(strFolder + @"\In");

foreach (string fileName in fileEntries)

{

msList.Add(new MemoryStream(File.ReadAllBytes(fileName)));

}

//Chuck of code to try in-memory concatenation

MemoryStream ms = new MemoryStream();

ms.Position = 0;

pdfEditor.Concatenate(msList.ToArray(), ms); //write output to MemoryStream

FileStream fsOut = File.OpenWrite(strFolder + @"\Out\outTest.pdf"); //then to disk

fsOut.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);

fsOut.Close();

//Chunck of code to try on-disk concatenation

//FileStream fsOut = new FileStream(strFolder + @"\Out\outTest.pdf", FileMode.Create);

//pdfEditor.Concatenate(msList.ToArray(), fsOut); //write output straight to disk.

fsOut.Close();

}

shahzad,

I now know that concatenated PDF files that are being created in our production environment are suffering the same corruption issue as I reported ealier today....

Adobe error: "There was an error processing a page. There was a problem reading this document (18)."

The issue only seems to happen when there are more than two PDF files being concatnated. However, that could just be because the second page is a PDF created with Aspose. So the sequence is:Page 1: MRF, Page 2: Render Summary, Page 3-X: other MS files rendered to PDF by Aspose.

This escalates matters to a higher level of urgency for us. Despite many successful testing cycles, the concatenation is no longer working as expected.

I am attaching several of the corrupt files for your examination...and one that was successful in testing.

I suppose it goes without saying, but I really need a quick fix for this.

-Kyle Peters

Hi Kyle,

I have tried to concatenate multiple files using Aspose.Pdf.Kit for .NET 4.4.0 and it worked perfectly. Can you please try the latest version at your end and see if it helps at your end? However, if it doesn’t resolve your issue then please share the sample input files with us - the files you shared earlier are the output files. We’ll test the issue with your input files and update you accordingly.

We’re sorry for the inconvenience.
Regards,

shahzad,

I reverted back to Aspose.Pdf.Kit version 4.3.0.0, and all is much better today. Our production environment is back on track *WHEW*

Now, I think something is wrong with version 4.4.0.0, but you aren't having any issues with it. I don't know if it matters, but I am using Visual Studio 2005 to compile and debug.

I am attaching a sample solution that has the DLL files for both versions in the BIN\DEBUG directory (renamed for identification.) The sample solution also includes the actual PDF files that I am trying to concatenate - they are in the IO\IN directory.

Currently, my sample solution is built with 4.4.0.0, and breaks (i.e. throws error when opening the resulting concatenated PDF.) I implore you try my solution as I have packaged it (using the included 4.4.0.0 DLL that I downloaded from your site today). Then, check the resulting PDF in the IO\OUT directory - I get errors.

THEN, change the reference in the solution to the 4.3.0.0 DLL, and check those results in the IO\OUT directory - I get success.

I am really not trying to be a pain-in-the-butt. We just have a lot riding on your products, and tout many successes because of your products. Overall, I am very happy with Aspose. Unfortunately, you only get to hear my problems in this forum :-(

Regards,

-Kyle Peters

Hi Kyle,

First of all, I’m extremely sorry for the inconvenience caused due to these issues. Our team tries to make sure that any such inconvenience is avoided; however, due to a variety of the PDF sources and contents etc. certain scenarios might cause issues - just like this particular scenario.

Secondly, we’re very much thankful to you for sharing the input PDF files along with the sample project; this helped us reproduce the issue at our end and it has been logged as PDFKITNET-17307 in our issue tracking system. Our team is looking into this issue and we’ll try to resolve this issue the earliest possible.

If you find any other issues please do not hesitate to let us know; we’ll be glad to help you out.

We appreciate your cooperation.
Regards,






Thanks Kyle - We’ve just found the equivalent situation whilst using 4.5.0.0 of Pdf.Kit and have consequently rolled back to version 4.2.0.0 of Pdf.Kit and this has resolved the issue for us also.

As I previously stated, we rolled back to 4.3.0.0 with success (i.e. Not all the way back to 4.2.0.0). So I’ll just wait for a more stable version before upgrading. Don’t you agree?

Hi Kyle and Matthew,

Can you please try to test with the latest hot fix attached with this post? I couldn’t notice any problem with this hot fix. Please try it at your end and if you still find the issue, please do let us know.

We’re sorry for the inconvenience.
Regards,

The issues you have found earlier (filed as 17307) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Shazad,

I rediscovered (the hard way) a very important thing that needs to be done when using the Aspose.Pdf.Kit.PdfFileEditor.Concaentate(Stream[] inputStreams, Stream outputStream) method...ALWAYS rewind the stream before adding it the the inputStreams array...

================================

MemoryStream ms = new MemoryStream();

ms = StreamConvertedAttachment(fileName); //a call to my custom method

ms.Position = 0; //NOTE: It is imperative to reset input MemoryStream's Postion back to 0 [ZERO] before adding it to array!!!!

msList.Add(ms);

...

MemoryStream msMaster = new MemoryStream();

pdfEditor.Concatenate(msList.ToArray(), msMaster);

================================

It was be nice of Aspose to do the rewinding ( Stream.Position=0; ) inside their Concatenation() method so I don't have to remember to do it. :-)

Hi Kyle,

We’ll investigate this issue at our end in detail and you’ll be updated with the status accordingly.

We’re sorry for the inconvenience.
Regards,

Hi Kyle,

I have tested this issue at my end using the latest version but couldn’t reproduce this situation. I didn’t have to reset the Position to 0. The output file was successfully concatenated. Please try to use the latest version and if you still find the same issue then please share a small sample application along with sample PDF files which can help us reproduce this issue, so we could test it at our end.

We’re sorry for the inconvenience.
Regards,