V4.3.0.0 - PdfFileEditor.Concatenate closes MemoryStream

We are having the exact same issue with the latest update. We really need 4.3 as a fix for another problem we had with the text getting cut off was included in that release. When we downloaded 4.3.0.2 it fixed the close memory stream issue, but then Adobe will not open the file. With this many people having the same issue concatenating memory streams, I would think you could reproduce the problem on your end. We also have a large solutions where trying to extract useable code for you to test is going to be difficult.

Hi Matt,

Can you please try the sample project I shared in my previous post? If you still find the same issue at your end, please do let us know. Also, in order to understand the issue we need to look into the difference between our sample code and the code running at your end. I hope you can help us in this regard.

We’re sorry for the inconvenience.
Regards,

Thanks Shahzad. This sample allowed me to get to the bottom of the problem (I’m guessing most people here will be doing a similar thing).


v4.3.0.x seem to be much more sensitive to the position of the position of the stream when performing operations. A common requirement when concatenating multiple documents together is to insert blank pages to support duplex printing (I’ve posted this previously as something I’d like to see in Aspose.Pdf.Kit as currently I have a dependency on Aspose.Pdf solely to produce a blank page).

To perform duplexing you need to add a blank page to documents with an odd number of pages. In Aspose, I’ve done this by creating a PdfFileInfo from the stream and then using PdfFileEditor.Append. However both of these operations set the Position of the MemoryStream to the end and therefore in v4.3.0.x break either the Append or Concatenate method if you do not reset the position to the start of the stream.

This is to code I now have for duplexing.

var pdfFile = new PdfFileInfo(inStream);
BlankPdfStream.Position = 0;
inStream.Position = 0;
if (pdfFile.NumberofPages % 2 == 1)
{
pdfFileEditor.Append(inStream, BlankPdfStream, 1, 1, inStream);
BlankPdfStream.Position = 0;
inStream.Position = 0;
}

Hopefully this will help everyone else as well.

I created the attached sample program which is a rough copy of my application. The important parts (including the failures) are here:


Pay special attention to this block in the QRR.cs file the function is MergePDFs:
System.Diagnostics.Debug.WriteLine(“Length of Original PDF:” + currentPDF.Length);
if (_file.memStream_PDF != null)
edt.Concatenate(currentPDF, _file.memStream_PDF, pdfOutput);
else
return pdfOutput = currentPDF;

//This length is 0 for some reason AFTER the second time this is called, first time through it actually appears to merge.
System.Diagnostics.Debug.WriteLine(“Length of Output PDF:” + pdfOutput.Length);


Taking Paul’s suggestion it looks like the following is now required for the subsequent calls:


System.Diagnostics.Debug.WriteLine(“Length of Original PDF:” + currentPDF.Length);

pdfOutput.Position = 0;
currentPDF.Position = 0;
if (_file.memStream_PDF != null)
edt.Concatenate(currentPDF, _file.memStream_PDF, pdfOutput);
else
return pdfOutput = currentPDF;

//This length is 0 for some reason AFTER the second time this is called, first time through it actually appears to merge.
System.Diagnostics.Debug.WriteLine(“Length of Output PDF:” + pdfOutput.Length);


When I added the highlighted lines into my code I was able to get a working pdf

Hi Paul and Eric,

Thank you very much for your feedback. We’ll investigate this problem at our end and you’ll be updated shortly.

We’re sorry for the inconvenience.
Regards,

Hi,

I too have the same problem – the code i am using is
MemoryStream[] outBuffer = pdfEditor.SplitToPages(mStream);
for (int PCount = 0; PCount <= TotalPages - 1; PCount++)
{
Byte[] _DocumentByte = outBuffer[PCount].GetBuffer();
inMemoryStream = new MemoryStream(_DocumentByte);
inMemoryStream.Write(_DocumentByte, 0, 0);
if (outStream.Length > 0)
{
inStreamsArray = new Stream[] { outStream, inMemoryStream };
}
else
{
inStreamsArray = new Stream[] { inMemoryStream };
}
pdfEditor.Concatenate(inStreamsArray, outFileStream);
outStream = outFileStream;
}
Byte[] _theByte = outFileStream.GetBuffer();
SavePdf(_theByte);

After I concatenate it gives an error of using closed stream.

Could any one of you help me out.

thanks & Regards,
Ramanath guptha

Hi Ramanath,

Please try the hot fix attached with the following post of this thread:
V4.3.0.0 - PdfFileEditor.Concatenate closes MemoryStream

Also, please make sure that you set the position of all the input streams to 0 i.e. inStream.Position = 0;

Please try this at your end; however if you still find any issues, please do let us know.

We’re sorry for the inconvenience.
Regards,


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


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