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:
public void CrearPDF(DataSet ds, string nombrearchivopdf)
{
StrongTypedReport cr = new StrongTypedReport();
cr.SetDataSource(ds);
Stream input = cr.ExportToStream(
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
FileStream output = new FileStream(nombrearchivopdf,
FileMode.Create);
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while((numBytes = input.Read(bytes, 0, size)) > 0)
output.Write(bytes, 0, numBytes);
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.
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, questions, are wellcome.
Submit questions related with current article to the article forum.