- Rick van den Bosch/
- Blog/
- Howto: present a Crystal Reports report as a PDF in an ASP.NET web application (without using temporary files)/
Howto: present a Crystal Reports report as a PDF in an ASP.NET web application (without using temporary files)
This one is actually quite simple: Crystal Reports supports exporting to a PDF. Exporting can be done to disk, or to a stream. And a stream can be written to the Repsonse property of a page. And there you go ð
// Variable declaration
  CrystalTest crystalTest;
  MemoryStream memoryStream;
// Create a new instance of the report you want to display as a PDF
  crystalTest = new CrystalTest();
// TODO: Add some stuff here to fill the report with data
// Export the report to a stream with the PDF format
  memoryStream = (MemoryStream)crystalTest.ExportToStream(ExportFormatType.PortableDocFormat);
// Set the ContentType to pdf, add a header for the length
// and write the contents of the memorystream to the response
  Response.ContentType = âapplication/pdfâ;
  Response.AddHeader(âcontent-lengthâ, Convert.ToString(memoryStream.Length));
  Response.BinaryWrite(memoryStream.ToArray());
//End the response
  Response.End();