Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Report of Crystal in PDF...How open in new tab or page?

I did a code to generate a report of Crystal Reports in PDF...But it opens in the same page of the user did a search and clicked in the button...Have any ways to open the PDF in a new tab or page ?

My code is:

private void OpenPDF()
{
    ReportDocument Rel = new ReportDocument();
    Rel.Load(Server.MapPath("../Reports/Test.rpt"));
    BinaryReader stream = new BinaryReader(Rel.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(stream.ReadBytes(Convert.ToInt32(stream.BaseStream.Length)));
    Response.Flush();
    Response.Close(); 
}

Thanks for the help!

like image 894
JohnyMoraes Avatar asked Feb 07 '12 16:02

JohnyMoraes


People also ask

How do I add a new page in Crystal Reports?

Go to the Section Expert. In your report footer, set it to create a new page before it is printed (In 'Section Expert', select the Report Footer -> 'Paging' tab -> Check 'New Page Before' checkbox). Following the above directions will give you one extra page at the end of the report.


2 Answers

In its most simplest interpretation, to open a new window or tab, the hyperlink to the page should have the target attribute set to "_blank".

<a href="GeneratePDF.aspx" target="_blank">Link to open PDF in new window</a>

Or you could create some Javascript that opens a new window instead. Make sure you call the Javascript function somewhere on the page.

<script type="text/javascript">
function loadPDF() {
   window.open('GeneratePDF.aspx','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
}
</script>

Or this code will inform the web browser that the file is a download (rather than a page to view inside the browser window). I think this is the best approach because the user gets the choice of Opening or Saving the PDF. So this does not do what you're asking for, but you might think it's better.

private void OpenPDF(string downloadAsFilename)
{
    ReportDocument Rel = new ReportDocument();
    Rel.Load(Server.MapPath("../Reports/Test.rpt"));
    BinaryReader stream = new BinaryReader(Rel.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment; filename=" + downloadAsFilename);
    Response.AddHeader("content-length", stream.BaseStream.Length.ToString());
    Response.BinaryWrite(stream.ReadBytes(Convert.ToInt32(stream.BaseStream.Length)));
    Response.Flush();
    Response.Close(); 
}
like image 105
Roy Goode Avatar answered Oct 02 '22 00:10

Roy Goode


Just write this code on button ::: OnClientClick="target = '_blank';"

like image 30
Sarowar Hossen Avatar answered Oct 02 '22 00:10

Sarowar Hossen