How to convert Crystal report ReportDocument object to Byte Array

2

(In order to Enlarge images Please Right Click and Open images in New Tab)

h1

 
//Sandip Patil Code Snippet

CrystalDecisions.CrystalReports.Engine.ReportDocument rpt =CrystalReportsHelper.CreateReport(System.Web.Hosting.HostingEnvironment.MapPath (@”~/Reports/Crystal/PatientStatement.rpt”));

rpt.SetParameterValue(“@BeginDate”, DateTime.Now);
rpt.SetParameterValue(“@EndDate”, DateTime.Now);

//Below method directly Sends pdf in Browser

rpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, response, false, string.Empty);

Now we will see How to convert Report object to Byte Array

byte[] getBytes = null;
System.IO.MemoryStream ms = (MemoryStream)rpt.ExportToStream (ExportFormatType.PortableDocFormat);
getBytes = ms.ToArray();

Now this Byte array can be used for PDF Report Generation
[HttpPost]
[Route(“GeneratePatientStatementReport”)]
public HttpResponseMessage GeneratePatientStatementReport()
{
byte[] getBytes = null;
System.IO.MemoryStream ms = (MemoryStream)rpt.ExportToStream (ExportFormatType.PortableDocFormat);
getBytes = ms.ToArray();
HttpResponseMessage resp;
resp = Request.CreateResponse(HttpStatusCode.OK);
resp.Content = new ByteArrayContent(getBytes);
resp.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue(“attachment”);
resp.Content.Headers.ContentDisposition.FileName = “file.name” + “.pdf”;
return resp;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s