Concatenated PDF file is damaged after download in ASP.NET Web Application client

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)

@GEWA-COMP_GmbH

Can you please try to test the scenario at your end using 24.3 version and below approach for PDF concatenation?

Please let us know what results you get by using above suggestions. We will further proceed accordingly.

Hello,

I have installed version 24.3. Unfortunately that doesn’t help either.

I tried this code. Same result. Only after donwload by client the file is damaged. See also attachment.

private byte[] MergeStreamsAsArrayIntern(List<MemoryStream> pdfStreams) //, Dictionary<int, string> bookmarkPlaces)
        {
            try
            {
                System.Diagnostics.Trace.TraceInformation("PdfWrapper.MergeStreams, try to merge " + pdfStreams.Count.ToString() + " pdf-streams.");

                Document firstDoc = new Document(pdfStreams[0]);
                for(int i=1; i<pdfStreams.Count; i++)
                {
                    Document mergedDoc = new Document(pdfStreams[i]);
                    firstDoc.Pages.Add(mergedDoc.Pages);
                }

                MemoryStream outStream2 = new MemoryStream();
                firstDoc.Save(outStream2);
                return outStream2.ToArray();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError("PdfWrapper.MergeStreams, Error by merge " + pdfStreams.Count.ToString() + " streams: " + ex.Message);
            }

            return null;
        }

Best regards

docuMerge_downloaded_ver.24.3.pdf (5.0 MB)

docuMerge_ver.24.3.pdf (4.9 MB)

@GEWA-COMP_GmbH

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56992

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.