Subject: BinaryWrite of merged pdf byte array causes error
Date: 2011-03-17 09:39:46
From: Bob Pope
Source: binarywrite-merged-pdf-byte-array-causes-error
----------------------------------------------------------------------

I have generated a pdf from SQL Server Reporting services and I have merged it with attachments and then saved those attachments to a file on disk, the saved file is opened by Acrobat Reader without error.

When I try to write binary array to an HttpResponse stream with Response.BinaryWrite(reportByteArray) I get the error: "The file is corrupted and cannot be repaired" from the Acrobat Reader plugin inside IE8. The code I am using follows:
        ///<summary>
        /// This is how we write PDF stream to response do not throw this code.
        ///</summary>
        ///<remarks></remarks>
        void WriteToStream(int rowID)
        {
            try
            {
                //bytes = rptvRH.LocalReport.Render("PDF" _
                //                                , Nothing _
                //                                , mimeType _
                //                                , encoding _
                //                                , extension _
                //                                , streamids _
                //                                , warnings)
 
                string[] objstreamids;
                string objmimeType;
                string objencoding;
                string objextension;
                byte[] objbytes;
                Microsoft.Reporting.WebForms.Warning[] warnings;
 
                objbytes = ReportViewer.LocalReport.Render("PDF"
                                                                 , null
                                                                 , out objmimeType
                                                                 , out objencoding
                                                                 , out objextension
                                                                 , out objstreamids
                                                                 , out warnings);
                objbytes = MergeAttachments(objbytes, rowID);
 
                //'Set content type
                Response.ContentType = "application/pdf";
 
                //' IE & Acrobat seem to require "content-disposition" header being in the response.  If you don't add it, the doc still works most of the time, but not always.
                //' this makes a new window appear: Response.AddHeader("content-disposition","attachment; filename=MyPDF.PDF");
                Response.AddHeader("content-disposition""inline; filename=MyPDF.PDF");
                Response.BinaryWrite(objbytes);  // Write the PDF stream out
                Response.Flush();             // Send all buffered content to the client
            }
            catch (Exception ex)
            {
                WriteGenericException(ex);
            }
        }
        /// <summary>         /// Take the created report and add attachments         /// </summary>         /// <param name="masterReport"></param>         /// <returns></returns>         private byte[] MergeAttachments(byte[] masterReport, int rowID)         {             //get the attachments             var _dataContext = new ReportEntities();             var _attachments = from a in _dataContext.Expense_Report_Attachments                                where a.Master_Row_ID == rowID                                select a;             //put the attachments into streams             List<MemoryStream> sa = new List<MemoryStream>();             MemoryStream mr = new MemoryStream(masterReport);             sa.Add(mr);             foreach (var item in _attachments)             {                 var receipt = item.Image;                 MemoryStream ms = new MemoryStream(receipt);                 sa.Add(ms);             }             //merge the masterReport and attachments             PdfDocument FullReport = new PdfDocument();             PdfDocumentBase.Merge(FullReport, sa.ToArray());             MemoryStream fr = new MemoryStream();             FullReport.Save(fr);             string file = Server.MapPath("/Temp") + @"\FullReport.pdf";             FullReport.Save(file);

            //Return to the caller             return fr.ToArray();         }
---------------------------------------------------------------------- Note: This question has been asked on the Q&A forum of Thang Dang's fraudulent ComponentPro brand If you purchased anything from ComponentPro, you have been scammed. Contact the payment processor who sold you the license and ask for your money back. Back to ComponentPro Q&A Forum Index