zoukankan      html  css  js  c++  java
  • 输出PDF文档到ASP.NET页面

    项目中的WORD报表文档最终会被转换为PDF文档,输出到WEB页面中,下面的代码是读取PDF文件并写入到页面输出流中:

    protected void Page_Load(object sender, EventArgs e)
        
    {
            
    try
            
    {
                
    //query the pdf path
                string pdfna = this.Request.QueryString["pdf"];
                
    if (pdfna == null)
                    
    throw new Exception("This page can not be accessed directly");
                
    string pdf = "Reports\\" + pdfna;
                pdf 
    = Server.MapPath(pdf);

                
    //read all bytes
                if (File.Exists(pdf) == false)
                    
    throw new Exception("This report is not created");
                FileStream fs 
    = File.Open(pdf, FileMode.Open);
                
    byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 
    0, buffer.Length);
                fs.Close();

                
    //write to response
                Response.ContentType = "application/pdf";
                Response.AddHeader(
    "content-disposition""filename=" + pdfna);
                Response.AddHeader(
    "content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
            }

            
    catch (Exception ex)
            
    {
                Response.Write(ex.Message);
            }

            
    finally
            
    {
                Response.Flush();
                Response.Close();
                Response.End();
            }

        }
  • 相关阅读:
    phpwind管理权限泄露漏洞
    CGI Hack与Webshell研究资料整理
    深入浅出net泛型编程[转载]
    加上checkbox的treeview控件源程序
    BCB消息消息机制
    开源ZPU介绍
    带复选框可以多选的组合框控件 TCheckCombobox,非常完美
    别人用delphi写的很简单实用的多列功能的treeview treelistview
    智能DVR视频监控系统,源代码
    delphi事件参数sender的用法例程
  • 原文地址:https://www.cnblogs.com/swnuwangyun/p/841786.html
Copyright © 2011-2022 走看看