![]() |
Languages »
C# »
General
Intermediate
Generate a PDF File using CrystalReportsBy rmortega77Very simple code to generate a PDF document using the CrystalReports engine. |
C#, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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();
}
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |