Printing .pdf reports in asp.net

Source:  Printing .pdf reports in asp.net    Tag:  scid pdf

How to print pdf reports in asp.net? I want the database data to open in the browser in the pdf format.

Your urgent help will be appreciated.

Thanks

The way I'm donig this is I have the form create the report and save it to the server silently. Then it also exports it to a pdf silently and displays that.

Eric


Hi,

I want the database data to open in the browser in the pdf format ?

You can use below function to open your Data in any content word, PDF. What you need to do is simple pass the Byte Data and set the mimeType to PDF.Below code will give a startup.

public static void writeBinaryStreamToResponse(String fileName, String mimeType, System.IO.Stream st,bool inline, HttpResponse response)
{
response.Clear();
response.ContentType = mimeType;
response.AddHeader("Content-Type", mimeType);
response.AddHeader("Content-Length", st.Length.ToString());

if(inline)
{
response.AddHeader("Content-Disposition","inline;filename="+fileName);
}
else
{
//There are a number of bugs that deal with PDF's being downloaded
//by IE over SSL. The following headers attempt to address the issue,
//however, the IE configuration item 'Do not save encrypted pages to disk'
//must be unchecked for them to work.
//http://support.microsoft.com/default.aspx?scid=kb;EN-US;321532
response.AddHeader("Accept-Ranges","bytes");
//http://support.microsoft.com/default.aspx?scid=kb;en-us;812935&Product=ie600
response.AddHeader("Cache-Control","private");
response.AddHeader("Pragma","expires");

response.AddHeader("Content-Disposition","attachment;filename="+fileName);
}
if(stis System.IO.MemoryStream)
{
((System.IO.MemoryStream)st).WriteTo(response.OutputStream);
}
else
{
long bufLen = 1024*128;
byte[] buffer =new byte[bufLen];
int bytesRead;
while ((bytesRead = st.Read(buffer, 0, buffer.Length)) > 0)
{
response.OutputStream.Write(buffer, 0, bytesRead);
}
}
response.Flush();
response.End();
}

If you want to print the data directly into the printer..One of the approach is to create a crystal report and you the print command
Here is the snippet :

publicstaticvoid PrintReport(Engine.ReportClass crToExport,int Copies)

{

System.Drawing.Printing.PrinterSettings printerSettings =new System.Drawing.Printing.PrinterSettings();

printerSettings.PrinterName = System.Configuration.ConfigurationSettings.AppSettings.Get("PrinterPort");

crToExport.PrintOptions.PrinterName = printerSettings.PrinterName;

int numCopies = (Copies==0?1:Copies);crToExport.PrintToPrinter(numCopies,false, 0, 0);

}