Click here to Skip to main content
6,595,444 members and growing! (21,400 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Generate a PDF File using CrystalReports

By rmortega77

Very simple code to generate a PDF document using the CrystalReports engine.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:14 Nov 2006
Views:22,936
Bookmarked:20 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 2.64 Rating: 2.54 out of 5
2 votes, 18.2%
1
3 votes, 27.3%
2
2 votes, 18.2%
3
2 votes, 18.2%
4
2 votes, 18.2%
5

Introduction

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   

   cr.SetDataSource(ds);   
   
   // Export the report to a Stream, in PDF format   

   Stream input = cr.ExportToStream(
     CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);   
   // Open the output file Stream    

   FileStream output = new FileStream(nombrearchivopdf, 
                                      FileMode.Create);   
   
   // 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.

   output.Close();
}

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.

History

  • Nov. 13, 2006 - First version.

License

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

rmortega77


Member
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[]cimex.com.cu for any personal message: Ideas on new articles; bibliography about new APIs; any contribution you could be willing to make are very wellcome... He currently needs info about new Windows Vista Shell APIs.

Submit questions related with current article to the article forum.
Occupation: Software Developer
Company: CIMEX S.A.
Location: Cuba Cuba

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMy vote of 2 PinmemberAhsanS22:48 10 May '09  
Generalintegration of crystalreport in asp.net Pinmemberhr Sheik18:31 5 Jun '08  
Questionhi all, crystal reports in asp.net PinmemberHaroonrashed2:05 5 Jun '08  
GeneralCould you add to this Pinmemberinetfly1233:19 2 May '07  
Questionso? PinmembereRRaTuM0:30 21 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Nov 2006
Editor: Smitha Vijayan
Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project