Click here to Skip to main content
13,550,709 members
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 14 Nov 2006

Generate a PDF File using CrystalReports

, 14 Nov 2006
Rate this:
Please Sign up or sign in to vote.
Very simple code to generate a PDF document using the CrystalReports engine.


Some times we need to create a Portable Document Format and we are always trying to get fancy PDF converters. Here is a simple way to do it, just using the CrystalReports engine built-in export method.

Using the code

You need to create a CrystalReports reprt, and define the data connection, data set, and the layout. Suppose we have created the StrongTypedReport report. We would call the following CrearPDF method, passing the DataSet filled with the data to be shown, along with the file name of the PDF to be output:

/// <summary>
/// Create a PDF file from a CrystalReport report.
/// </summary>
/// <param name="ds">DataSet, same type used in the report</param>
/// <param name="nombrearchivopdf">PDF file
///            output archive name</param>
public void CrearPDF(DataSet ds, string nombrearchivopdf)
   StrongTypedReport cr = new StrongTypedReport();      
   // Set the report DataSet   
   // Export the report to a Stream, in PDF format   
   Stream input = cr.ExportToStream(
   // Open the output file Stream    
   FileStream output = new FileStream(nombrearchivopdf, 
   // Copy the buffer Stream to the ouput file   
   // We avoid the use of cr.Export, because crystal report   
   // do not inherits the current Thread permission   
   const int size = 4096; 
   byte[] bytes = new byte[4096]; 
   int numBytes;            
   while((numBytes = input.Read(bytes, 0, size)) > 0)         
      output.Write(bytes, 0, numBytes);   
   // Close the ouput file Stream.

Points of Interest

That's it! It works for me! The Stream input and output buffering used could be useful if you wish to redirect it to the Response Stream object, and let the browser directly show it, or let the user download it.


  • Nov. 13, 2006 - First version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer CIMEX S.A.
Cuba Cuba
Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[] for any personal message: Ideas on new articles, bibliography about new APIs, questions, are wellcome.

Submit questions related with current article to the article forum.

You may also be interested in...


Comments and Discussions

GeneralCould you add to this Pin
inetfly1232-May-07 2:19
memberinetfly1232-May-07 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 14 Nov 2006
Article Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid