We have a strange problem with a merged PDF file in an ASP.NET Web Application (NET Framework 4.7.2). We merge 2 PDF files. The merged file looks fine on the server and opens there without any errors. After downloading in the client (Chrome), the file is corrupted.
The Programm generates 2 sets of PDF-Documents (separately or together) and then send ready PDF as stream in Client-Response with help of the following code:
AsposeWrapper.PdfWrapper pdfWrapper = new AsposeWrapper.PdfWrapper();
byteArr = pdfWrapper.ConcatenateStreamsAsArray(new List<MemoryStream>() { docu1Stream, docu2Stream });
Response.Clear();
Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
Response.ContentType = "application/pdf";
Response.OutputStream.Write(byteArr, 0, byteArr.Length);
Response.OutputStream.Flush();
First set:
docu1.pdf - as it is generated on server
docu1_downloaded.pdf – as it is downloaded in client ( Chrom, is opened in internal viewer and than saved on disk)
Second set:
Docu2.pdf - as it is generated on server
Docu2_downloaded.pdf – as it is downloaded in client ( Chrom, is opened in internal viewer and than saved on disk)
First+Second sets:
docuConcat.pdf - as it is generated on server. On server docu1.pdf and docu2.pdf are converted to MemoryStream and than concatenated in the following function:
public byte[] ConcatenateStreamsAsArray(List<MemoryStream> pdfStreams)
{
PdfFileEditor editor = new PdfFileEditor();
MemoryStream outStream = new MemoryStream();
editor.Concatenate(pdfStreams.ToArray(), outStream);
Document pdfOutDocument = new Document(outStream);
Aspose.Pdf.Annotations.GoToAction goToAction = new Aspose.Pdf.Annotations.GoToAction(pdfOutDocument.Pages[1]);
pdfOutDocument.OpenAction = goToAction;
MemoryStream outStream2 = new MemoryStream();
pdfOutDocument.Save(outStream2);
return outStream2.ToArray();
}
docuConcat_downloaded.pdf – as it is downloaded in client ( Chrom, is opened in internal viewer and than saved on disk)
All this files, except docuConcat_douwnloaded.pdf, are OK and can be opened in AcrobatReader without problem.
If we try to open docuConcat_douwnloaded.pdf than shortly comes message like “File is damaged and will be repaired” (translation from German). If we try to close AcrobatReader, comes following message:
“Please save the changes…”
If we try to save file, comes following error-message
“The document could not be saved…”
Also it is not possible to fill existing Form-fields and save changes.
We have tried to check and repair (Document.Check(true)) concatenated document before write to Response. Also try to optimize with following OptimizationOption ({ LinkDuplcateStreams = true, RemoveUnusedObjects = true, RemoveUnusedStreams = true, AllowReusePageContent = true, UnembedFonts = true }), but without any success - resulted document on client-side is always damaged.
docu2_downloaded.pdf (4.8 MB)
docuConcat.pdf (4.9 MB)
docuConcat_downloaded.pdf (5.0 MB)
docu1_downloaded.pdf (186.7 KB)
docu2_downloaded.pdf (4.8 MB)
docu1.pdf (93.4 KB)