Concatenate Issue

I am trying to concatenate two System.IO.Streams that I get from Crystal Reports

I convert the streams to MemoryStreams then call the following:
My problem is the output pdf is 0 in length when both the source pdfs have > 0 in length.
If I save the memory streams to the hard disk I can then open the pdf file with no issue but for some reason the concatenate method is returning a 0 length stream.

What am I missing?

Thanks


private System.IO.MemoryStream MergePDFs(System.IO.MemoryStream _AdditionalPDF,
System.IO.MemoryStream currentPDF,
int i)
{
Aspose.Pdf.License lic;
Aspose.Pdf.Facades.PdfFileEditor edt;

System.IO.MemoryStream pdfOutput;
try
{
//license
lic = new Aspose.Pdf.License();
lic.SetLicense(“Aspose.Total.lic”);
//end license
pdfOutput = new System.IO.MemoryStream();
pdfOutput = currentPDF;


edt = new Aspose.Pdf.Facades.PdfFileEditor();


if (pdfOutput != null)
pdfOutput.Position = 0;

if (currentPDF != null)
currentPDF.Position = 0;

_AdditionalPDF.Position = 0;
edt.Concatenate(currentPDF, _AdditionalPDF, pdfOutput);

return pdfOutput;
}
catch (Exception ex)
{
throw new ApplicationException(null, ex);
}
finally
{
lic = null;
edt = null;

pdfOutput = null;
}
}

Hi Robert,

Thank you for the sample code, can you please share PDF documents after saving from memory streams. This will help us to investigate the exact issue and reply back to you soon.

We apologize for your inconvenience.

Thanks & Regards,

Attached is my sample pdf

For my early test I am must taking a copy of this and merging it with it self.

Hi Robert,

Thank you for sharing the template PDF document.

I have tested your scenario with your template file and sample code of Aspose.Pdf.Facades and it works fine (attached is the resultant file for your reference).

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Hello Eric,


Thanks for using our products. Adding more to Rashid’s comments, I have tested the scenario with upcoming release version of Aspose.Pdf for .NET 6.4.2 and I am also unable to notice any problem. I have made some changes to the code that you are shared and PDF files are properly being merged. For your reference, I have attached the resultant PDF document that I have generated. We apologize for your inconvenience.

[C#]
Aspose.Pdf.Facades.PdfFileEditor edt;
//output stream
FileStream outputStream = new FileStream(@“D:\pdftest\Concatenated-output.pdf”, FileMode.Create);
//input streams
FileStream inputStream1 = new FileStream(@“D:\pdftest\t.pdf”, FileMode.Open);
FileStream inputStream2 = new FileStream(@“D:\pdftest\Copy of t.pdf”, FileMode.Open);

try
{
edt = new Aspose.Pdf.Facades.PdfFileEditor();
edt.Concatenate(inputStream1, inputStream2, outputStream);

// return pdfOutput;
}
catch (Exception ex)
{
throw new ApplicationException(null, ex);
}
finally
{
lic = null;
edt = null;

inputStream1.Close();
inputStream2.Close();
outputStream.Close();
}

I too am using v6.4.2

One difference I am noticing is I am using MemoryStreams and you are using FileStreams. Could that be part of the issue?

I am still having this issue so I am not sure where to go next

Hello Eric,


I have again tested the scenario where I have tried loading the contents of source source PDF files into separate MemoryStream objects and then have saved the merged PDF file to another MemoryStream object. Once the documents have been merged and saved into MemoryStream object, we will save the final output over system using FileStream object. Please take a look over the following code snippet.

[C#]

// create
MemoryStream object for first file
MemoryStream
memStream1 =
new MemoryStream();
using
(
FileStream fileStream = File.OpenRead(@“D:\pdftest\t.pdf”)
{
//
set the memorystream lenght equal to input file length
memStream1.SetLength(fileStream.Length);
fileStream.Read(memStream1.GetBuffer(), 0, (int)fileStream.Length);
}

MemoryStream memStream2 = new MemoryStream();
using (FileStream fileStream = File.OpenRead(@"D:\pdftest\Copy of t.pdf"))
{
memStream2.SetLength(fileStream.Length);
fileStream.Read(memStream2.GetBuffer(), 0, (int)fileStream.Length);
}

Aspose.Pdf.Facades.PdfFileEditor edt;
//output stream
MemoryStream outputStream = new MemoryStream();

try
{
edt = new Aspose.Pdf.Facades.PdfFileEditor();
edt.Concatenate(memStream1, memStream2, outputStream);
// return pdfOutput;
}

catch (Exception ex)
{
throw new ApplicationException(null, ex);
}

finally
{
lic = null;
edt = null;
memStream1.Close();
memStream2.Close();
}

MessageBox.Show(outputStream.Length.ToString());
// Now we need to save the contents of Output MemoryStream to PDF file
FileStream fs = File.OpenWrite(@"D:\pdftest\Resultant-Concatenated.pdf");
// save the contents of MemoryStream to FileStream object
fs.Write(outputStream.ToArray(),0,(int)outputStream.Length);
// close the fileStream object
fs.Close();
// close memorystream object holding merged document.
outputStream.Close();

I am getting closer...

I am guessing my original problem had to do with the way I was creating a second pdf from Crystal Reports. I think there were some pointer issues that was preventing the concatenation from working. I created a second, seperate report and I get a step further. The concatenation works and I can create a pdf that can be saved to disk and viewed. I am tyring however to output the concatenated stream to the web browser and have Adobe open. This works fine with the single pdf created from Crystal Reports. The Concatenated pdf throws the following error:

File does not beign with '%PDF-'.

For some reason the concatenated pdf causes this?

Also, I should add that I am now using Aspose.Pdf v6.5.0.0

Hi Eric,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for the details.

However, to exactly replicate your issue at our end, we would appreciate it if you can create a sample web application with your template files and post here to show us the issue. This will help us regenerate the issue and get to the cause of the issue soon.

Sorry for any inconvenience caused,

Attached is the sample project which shows the error

I tried this with the older Aspose.Pdf.Kit v6.0.0.0 and everything works fine. Looks like an issue with the new dll

Hi Eric,

Thank you for the sample applicatoin, kindly add Response.AddHeader line in "Page_Load" method as mentioned below. By adding this line of code, error "File does not begin with '%PDF-'" is not coming.

[C#]

protected void Page_Load(object sender, EventArgs e)
{
byte[] buffer;
System.IO.Stream _ReportStream;
_ReportStream = Merge();

buffer = new byte[_ReportStream.Length];
_ReportStream.Read(buffer, 0, buffer.Length);

System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=ConversionResult.pdf;");
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", buffer.Length.ToString());
System.Web.HttpContext.Current.Response.BinaryWrite(buffer);
}

Please feel free to contact support in case you face any problem.

Thanks & Regards,

I added the line of code you suggested and now I see two new items

1) I am promted to save or open the pdf (where with pdf.kit it just opens the pdf)

2) If I open the pdf I get an error in Adobe: "Adobe reader could not open 'ConversionResult[1].pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded). If I choose the save option then try to open the file I get the same error again. I have attached the file that was saved.

Hi Robert,

Thank you for sharing the sample application and resultant document. I have tested the scenario and I am able to notice the same problem. For the sake of correction, I have logged this problem as PDFNEWNET-32674 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction.

We apologize for your inconvenience.

Thanks & Regards,

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


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