C# 如何在“新建”选项卡中打开报表查看器生成的pdf?
我编写了一段代码来创建PDF并下载它C# 如何在“新建”选项卡中打开报表查看器生成的pdf?,c#,asp.net,pdf,reportviewer,C#,Asp.net,Pdf,Reportviewer,我编写了一段代码来创建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
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
}
protectedvoid创建\u pdf\u单击(对象发送者,事件参数e)
{
警告[]警告;
字符串[]流线;
string mimeType=string.Empty;
字符串编码=string.Empty;
字符串扩展名=string.Empty;
字符串devinfo=“32350350PDF”+
“8.5英寸”+
“11英寸”+
“0.5英寸”+
“0.5英寸”+
“0英寸”+
“0英寸”+
"";
//设置报表查看器对象并获取字节数组
ReportViewer=新的ReportViewer();
viewer.ProcessingMode=ProcessingMode.Local;
viewer.LocalReport.ReportPath=Server.MapPath(“~/installants\u Report.rdlc”);
DataView dv=新的DataView();
DataTable dt=新的DataTable();
dv=(System.Data.DataView)SqlDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty);
dt=dv.ToTable();
Add(newreportdatasource(“DataSet1”,dt));
byte[]bytes=viewer.LocalReport.Render(“PDF”、devinfo、out-mimeType、out-encoding、out-extension、out-streamid、out-warnings);
//现在已经有了表示PDF报告的所有字节,请将其缓冲并发送到客户端。
Response.Buffer=true;
Response.Clear();
Response.ContentType=mimeType;
AddHeader(“内容处置”、“附件;文件名=“+”分期付款“+”列表“+”+扩展名);
Response.BinaryWrite(字节);//创建文件
Response.Flush();//将其发送到客户端进行下载
}
有没有办法在新选项卡或页面中打开此PDF?
那是因为我需要一个按钮来直接打印报告
建议在新点击中打开pdf,然后用户可以打印它
还有其他建议吗
谢谢你的帮助* 我得到了答案
protected void Page_Load(object sender, EventArgs e)
{
string file_name = Request.QueryString["file"];
string path = Server.MapPath("Print_Files/"+file_name);
// Open PDF File in Web Browser
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
protected void Print_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.
string path =Server.MapPath("Print_Files") ;
Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7); // creates a number between 1 and 6
int card = rnd.Next(9); // creates a number between 0 and 51
string file_name = "Installments" + "List" + month+dice+card+".pdf"; //save the file in unique name
//3. After that use file stream to write from bytes to pdf file on your server path
FileStream file = new FileStream(path + "/" + file_name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
file.Write(bytes, 0, bytes.Length);
file.Dispose();
//4.path the file name by using query string to new page
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Print.aspx?file="+file_name));
}
完成!!!
希望此代码对其他人有所帮助:)
谢谢 我得到了答案
protected void Page_Load(object sender, EventArgs e)
{
string file_name = Request.QueryString["file"];
string path = Server.MapPath("Print_Files/"+file_name);
// Open PDF File in Web Browser
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
protected void Print_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.
string path =Server.MapPath("Print_Files") ;
Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7); // creates a number between 1 and 6
int card = rnd.Next(9); // creates a number between 0 and 51
string file_name = "Installments" + "List" + month+dice+card+".pdf"; //save the file in unique name
//3. After that use file stream to write from bytes to pdf file on your server path
FileStream file = new FileStream(path + "/" + file_name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
file.Write(bytes, 0, bytes.Length);
file.Dispose();
//4.path the file name by using query string to new page
Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "Print.aspx?file="+file_name));
}
完成!!!
希望此代码对其他人有所帮助:)
谢谢 使用带有target=“\u blank”的html锚定这会重定向到一个新页面,并在新页面的页面中加载生成报告?好的,但下载此代码时不会打开创建的pdf@PeetvdWesthuizen@PeetvdWesthuizen我需要在浏览器窗口内查看pdf,以强制打开pdf更改内容处置标题表单附件到内联:内容类型:应用程序/pdf内容配置:内联;filename.pdfy您可以在会话中保存字节[],并在新页面上使用它吗?使用带有target=“\u blank”的html锚点这会重定向到一个新页面,并在新页面的页面中加载生成报告?好的,但下载此代码时不会打开创建的pdf@PeetvdWesthuizen@PeetvdWesthuizen我需要在浏览器窗口内查看pdf,以强制打开pdf更改内容处置标题表单附件到内联:内容类型:应用程序/pdf内容配置:内联;filename.pdfy您可以将字节[]保存在会话中,并在新页面上使用它(可能?Thanx到@PeetvdWesthuizen;)在我没有添加子报告之前,它工作正常。任何人都知道如何在同一场景中处理子报告。Thanx to@PeetvdWesthuizen;)在我没有添加子报告之前,它工作正常。任何人都知道如何在相同的场景中处理子报表。