在PDF中生成Crystal的报告...如何在新标签或页面中打开?

时间:2012-02-07 16:56:26

标签: c# visual-studio-2010 pdf crystal-reports

我做了一个代码来生成PDF格式的Crystal Reports报告......但它在用户的同一页面打开并进行了搜索并点击了按钮...有办法在新的中打开PDF标签或页面?

我的代码是:

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(); 
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:7)

在最简单的解释中,要打开新窗口或标签页,指向页面的超链接应将target属性设置为"_blank"

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

或者您可以创建一些可以打开新窗口的Javascript。确保在页面的某处调用Javascript函数。

<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>

或者此代码将通知Web浏览器该文件是下载文件(而不是在浏览器窗口中查看的页面)。我认为这是最好的方法,因为用户可以选择打开或保存PDF。所以这不符合你的要求,但你可能认为它更好。

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(); 
}

答案 1 :(得分:1)

只需在按钮上编写此代码::: OnClientClick =“target ='_ blank';”