WEB利用PDF实现打印和排版

来源:互联网 发布:淘宝卖家借贷额度 编辑:程序博客网 时间:2024/05/22 18:22

大家都知道WEB上,打印是天然的限制,各种不方便,这里不一一阐明了,即使用了JS控制,水晶报表打印等还是一样,这里介绍一种方法,利用PDF和ADOBE READER实现的打印功能。


AdbeRdr大家都使用过,不知道大家有没有发现,它有一个插件,当IE打开PDF时,直接以它打开,而它的各种功能都非常齐,这就提供了我们一个思路,利用PDF来实现,这里来说一点,就是生成PDF,网上有PDF生成类,但我用的是水晶报表,用它可以实现各种效果,图文例表,多行,各种统计图和列表结合等,我们来看一下普通的水晶报表来导出PDF是怎么实现的。


            CrystalDecisions.Web.CrystalReportSource crs = new CrystalDecisions.Web.CrystalReportSource();
            ExportOptions crExportOptions;
            CrystalDecisions.CrystalReports.Engine.ReportDocument crReportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            string temp_rpt = Server.MapPath("/rpt/xxxxxxxxxx.rpt");
            crReportDocument.Load(temp_rpt);//加截


            CrystalDecisions.Shared.DiskFileDestinationOptions crDiskFileDestinationOptions;


            string Fname;
            string FileName = "temp.pdf";
            string _SystemPath = Request.PhysicalApplicationPath + "ExportPath\\";
            Fname = _SystemPath + FileName;//导出路径


            crDiskFileDestinationOptions = new CrystalDecisions.Shared.DiskFileDestinationOptions();
            crDiskFileDestinationOptions.DiskFileName = Fname;


            crExportOptions = crReportDocument.ExportOptions;


            crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
            crExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
            crExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;


//列表的加载
            int asd = ds.Tables[0].Rows.Count;//总记录数


            DataTable dtx1 = dtx(ds.Tables[0]);///转换逻辑,这里反正是列表等加载,每人都有自己的需求,不一定看我的。


            dtx1 = fill_name(dtx1);再次转换逻辑


            crReportDocument.SetDataSource(dtx1);//绑定


            crReportDocument.SetParameterValue("par1", "统计时间: " + Convert.ToDateTime(Session["b_time"].ToString()).ToString("yyyy-MM-dd") + " - " + Convert.ToDateTime(Session["e_time"].ToString()).ToString("yyyy-MM-dd"));
            crReportDocument.SetParameterValue("par2", "操作时间: " + DateTime.Now.ToString("yyyy-MM-dd"));//两参数设置


            crReportDocument.Export();//输出


            try
            {
                string pdf = "\\ExportPath\\" + FileName;
                pdf = Server.MapPath(pdf);
                if (System.IO.File.Exists(pdf) == false)
                {
                    throw new Exception("目录没有存在!");
                }


                System.IO.FileStream fs = System.IO.File.Open(pdf, System.IO.FileMode.Open);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();


                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "filename=" + pdf);
                Response.AddHeader("content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                throw;
            }
            finally
            {
                Response.Flush();
                Response.Close();
                Response.End();
            }



这是一个简单的列表加载转PDF并在WEB上输出,我已经在代码上做了注释,既然新手也可以一目了然,

0 0