在PDF中生成水晶报告...如何在新标签或页面中打开?(Generate Report of Crystal in PDF…How open in new tab or page?)

编程入门 行业动态 更新时间:2024-10-27 05:36:23
在PDF中生成水晶报告...如何在新标签或页面中打开?(Generate Report of Crystal in PDF…How open in new tab or page?)

我做了一个代码来生成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(); }

谢谢您的帮助!

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!

最满意答案

在最简单的解释中,要打开一个新的窗口或选项卡,页面的超链接应该将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>

或者这段代码会通知网络浏览器该文件是一个下载文件(而不是浏览器窗口内的页面)。 我认为这是最好的方法,因为用户可以选择打开或保存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(); }

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

更多推荐

本文发布于:2023-07-17 07:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水晶   标签   页面   报告   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!