【发布时间】:2013-10-01 12:19:00
【问题描述】:
我编写了一个代码来创建 PDF 并下载它
protected void create_pdf_Click(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
string devinfo = "<DeviceInfo><ColorDepth>32</ColorDepth><DpiX>350</DpiX><DpiY>350</DpiY><OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>0.5in</MarginLeft>" +
" <MarginRight>0in</MarginRight>" +
" <MarginBottom>0in</MarginBottom>" +
"</DeviceInfo>";
// Setup the report viewer object and get the array of bytes
ReportViewer viewer = new ReportViewer();
viewer.ProcessingMode = ProcessingMode.Local;
viewer.LocalReport.ReportPath = Server.MapPath("~/Installments_Report.rdlc");
DataView dv = new DataView();
DataTable dt = new DataTable();
dv = (System.Data.DataView)SqlDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty);
dt = dv.ToTable();
viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
byte[] bytes = viewer.LocalReport.Render("PDF", devinfo, out mimeType, out encoding, out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + "Installments" + "List" + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download
}
有什么方法可以在新标签页或新页面中打开此 PDF 吗?
那是因为我需要有按钮来直接打印报告
建议在新的水龙头中打开 pdf 然后用户可以打印它
还有什么建议!!?
感谢您的帮助!*
【问题讨论】:
-
使用带有 target="_blank" 的 html 锚点重定向到新页面并在新页面的 Page_Load 中生成报告?
-
好的,但是这个下载代码不能打开创建的 pdf @PeetvdWesthuizen
-
@PeetvdWesthuizen 我需要在浏览器窗口中查看 pdf
-
强制打开 pdf 将 Content-Disposition Header 表单附件更改为 inline : Content-Type: application/pdf Content-Disposition: inline;文件名.pdf
-
您可以将 byte[] 保存在会话中并在新页面上使用它吗?
标签: c# asp.net pdf reportviewer