如何在新选项卡中打开报告查看器生成的 pdf? [英] how can i open pdf generated by report viewer in new tab?

查看:55
本文介绍了如何在新选项卡中打开报告查看器生成的 pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个代码来创建 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 吗?

那是因为我需要有按钮来直接打印报告

建议在新的tap中打开pdf然后用户可以打印

the suggestion that to open the pdf in new tap then user can print it

还有什么建议!!?

感谢您的帮助!*

推荐答案

我有答案

  1. 首先,您需要从报告查看器创建 pdf 文件并保存服务器上的某处
  2. 您需要以唯一名称保存文件
  3. 之后使用文件流将字节写入服务器路径上的 pdf 文件
  4. 使用查询字符串将文件名路径到新页面
  5. 在 Page_Load 中从查询字符串中获取 pdf 文件名并打开它

  1. Firstly You Need To create pdf file from report viewer and Save it somewhere on server
  2. you need to save the file in unique name
  3. After that use file stream to write from bytes to pdf file on your server path
  4. path the file name by using query string to new page
  5. in Page_Load qet the pdf file name from query string and open it

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

在Page_Load中从查询字符串中获取pdf文件名并在Web浏览器中打开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);
    }
 }

完成!!!希望此代码对某人有帮助:)谢谢!!!

Done !!! hope this code helpful to someone :) Thanks!!!

这篇关于如何在新选项卡中打开报告查看器生成的 pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆