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

        }
  • 相关阅读:
    RecyclerView中装饰者模式应用
    Android中的Drawable和动画
    Android的线程和线程池
    Bitmap的加载和Cache
    Android的消息机制
    【Java基础】线程和并发机制
    Asp.Net 将HTML中通过dom-to-image.js标签div内的内容转化为图片保存到本地
    Asp.Net MVC @Html.TextBox 只允许输入数字问题
    程序员编程10大原则,请牢牢记住
    Asp.Net MVC WebAPI的创建与前台Jquery ajax后台HttpClient调用详解
  • 原文地址:https://www.cnblogs.com/swnuwangyun/p/841786.html
Copyright © 2020-2023  润新知