码迷,mamicode.com
首页 > 其他好文 > 详细

利用itextsharp导出pdf

时间:2015-01-30 22:57:12      阅读:539      评论:0      收藏:0      [点我收藏+]

标签:itextsharp   导出pdf   

在项目中,客户要求把一些表格导出pdf格式,在网上找了好久终于发现一个完美的解决方案,利用itextsharp可以很方便的导出pdf文件,不说太多了,直接上图,有图有真相

首先是页面图片

技术分享


然后是导出的pdf图片

技术分享

导出pdf代码如下:

 //导出Mold pdf文档
        public void exportpdf_Mold(string quotenum, string fullpath)
        {
            string pdfName = quotenum + ".pdf";
            string savePath = "ExcelTemplate/" + pdfName;

            //利用iTextSharp导出pdf
            Rectangle rec = new Rectangle(792, 612);
            Document document = new Document(rec);
           
            document.SetMargins(18f, 18f, 36f, 36f);
            PdfWriter pdfwriter = PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath(savePath), FileMode.Create));
            document.Open();

            PdfPTable pdftable = new PdfPTable(10);
            pdftable.HorizontalAlignment = 1;           

            float[] cellwidth = { 5f, 4.5f, 3f, 3.5f, 2.5f, 2.6f, 2.5f, 2.5f, 2.5f, 2.8f };
            pdftable.SetWidths(cellwidth);


            //从数据库中读取tooling cost数据
            string strSql = "select * from tooling_cost where quotenum=‘" + quotenum.Substring(2) + "‘ order by id asc";
            DataTable dt_tooling = CommonMySql.MySql_Select(strSql);

            strSql = "select * from part_cost where quotenum=‘" + quotenum.Substring(2) + "‘ order by id asc";
            DataTable dt_part = CommonMySql.MySql_Select(strSql);

            int newfamily = 0; //记录是否是新的familymold
            int oldfamily = 0; //记录是否是老的familymold

            //添加表头          
            Font pdffont_i = FontFactory.GetFont("Times New Roman", 14, Font.ITALIC);
            Font pdffont_b = FontFactory.GetFont("Arial", 22, Font.BOLD);
            Font pdffont_h = FontFactory.GetFont("Arial", 12);
            Font pdffont = FontFactory.GetFont("Arial", 10);

            Image jpeg = Image.GetInstance(HttpContext.Current.Server.MapPath("Images/logo.jpg"));
            PdfPCell cell = new PdfPCell(jpeg);
            cell.FixedHeight = 40f;
            cell.Colspan = 3;
            cell.Rowspan = 4;
            cell.Border = 0;
            pdftable.AddCell(cell);

            cell = new PdfPCell(new Phrase(("ICOQuote"),pdffont_b));
            cell.Colspan = 3;
            cell.Rowspan = 2;
            cell.Border = 0;
            cell.BackgroundColor = new BaseColor(0xFF, 0xFA, 0xCD);
            cell.HorizontalAlignment = 1;
            pdftable.AddCell(cell);

            cell = new PdfPCell(new Phrase("\nQuote: " + quotenum + "\nDate: " + DateTime.Now.ToString("MM/dd/yyyy") + "\n\n", pdffont));
            cell.Colspan = 4;
            cell.Rowspan = 2;
            cell.Border = 0;
            cell.BackgroundColor = new BaseColor(0xFF, 0xFA, 0xCD);
            cell.HorizontalAlignment = 1;
            pdftable.AddCell(cell);

            cell = new PdfPCell(new Phrase(""));
            cell.Colspan = 4;
            cell.Rowspan = 2;
            cell.Border = 0;
            cell.VerticalAlignment = 1;
            pdftable.AddCell(cell);

            cell = new PdfPCell(new Phrase("ICO Products, LLC\n" + "6415 Angola Rd\n" + "Holland,OH 43528\n" + "phone:419-867-3900\n" + "Fax:419-867-7200", pdffont));
            cell.Colspan = 3;
            cell.Rowspan = 2;
            cell.Border = 0;
            cell.VerticalAlignment = 1;
            pdftable.AddCell(cell);

            cell = new PdfPCell(new Phrase("Tooling", pdffont_i));
            cell.BackgroundColor = new BaseColor(0xC0, 0xC0, 0xC0);
            cell.Colspan = 10;
            cell.Padding = 5f;
            cell.HorizontalAlignment = 1;
            pdftable.AddCell(cell);

            string[] tooling_header = { "Part Description", "Material", "Color", "Surface Finish", "Tooling Config.", "Tooling Life", "Lead Time", "Sample Quantity", "Tooling Price", "Snap Shot" };
            for (int i = 0; i < tooling_header.Length; i++)
            {
                cell = new PdfPCell(new Phrase(tooling_header[i], pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.BackgroundColor = new BaseColor(0xC0, 0xC0, 0xC0);
                pdftable.AddCell(cell);
            }

            //写入数据
            for (int i = 0; i < dt_tooling.Rows.Count; i++)
            {
                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["partname"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["material"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["color"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["surfacefinish"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["cavities"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["toolinglife"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["leadtime"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_tooling.Rows[i]["quantity"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 4f;
                cell.PaddingTop = 4f;
                pdftable.AddCell(cell);

                newfamily = int.Parse(dt_tooling.Rows[i]["familymold"].ToString());
                //第一次进入某个familymold
                if (newfamily == 0 || oldfamily != newfamily)
                {
                    int familycount = 0;
                    if (newfamily != 0)
                    {
                        string sqlStr = "select count(id) from tooling_cost where quotenum=‘" + quotenum.Substring(2) + "‘ and familymold=" + newfamily;
                        familycount = CommonMySql.MySql_Select_int(sqlStr);
                    }

                    oldfamily = newfamily;                

                    cell = new PdfPCell(new Phrase("$" + Convert.ToInt32(dt_tooling.Rows[i]["toolingprice"]).ToString("N0"), pdffont));
                    cell.Rowspan = familycount;
                    cell.HorizontalAlignment = 1;
                    cell.VerticalAlignment = Element.ALIGN_MIDDLE;                  
                    pdftable.AddCell(cell);

                }

                //插入图片
                string snapshot = dt_tooling.Rows[i]["snapshot"].ToString();
                snapshot = snapshot.Substring(snapshot.LastIndexOf("/") + 1);
                string imgpath = fullpath + snapshot;
                Image jpeg1 = Image.GetInstance(imgpath);

                cell = new PdfPCell(jpeg1);
                cell.FixedHeight = 8f;               
                cell.HorizontalAlignment = 1;
                pdftable.AddCell(cell);

            }

            cell = new PdfPCell(new Phrase("\n"));
            cell.Colspan = 10;
            cell.HorizontalAlignment = 1;
            pdftable.AddCell(cell);

            cell = new PdfPCell(new Phrase("Parts", pdffont_i));
            cell.BackgroundColor = new BaseColor(0xC0, 0xC0, 0xC0);
            cell.Padding = 5f;
            cell.Colspan = 10;
            cell.HorizontalAlignment = 1;
            pdftable.AddCell(cell);

            string[] aprts_header = { "Part Description", "Part Size", "Part Weight", "Part Qty", "Price Ea.", "Price Ea. w/Priority Air(1~2 Days)", "Price Ea. w/Economy Air(3~5 Days)", "Price Ea. w/Ocean +Domestic Air(15~21 Days)", "Price Ea. w/Ocean to Port(21~35 Days)", "Closest Port" };

            for (int i = 0; i < aprts_header.Length; i++)
            {
                if (i >= 5 && i <= 8)
                {
                    Font pdftempfont = FontFactory.GetFont("Arial", 8);
                    cell = new PdfPCell(new Phrase(aprts_header[i], pdftempfont));
                }
                else
                {
                    cell = new PdfPCell(new Phrase(aprts_header[i], pdffont));
                }

                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;              
                cell.BackgroundColor = new BaseColor(0xC0, 0xC0, 0xC0);
                pdftable.AddCell(cell);
            }

            //写入数据
            for (int i = 0; i < dt_part.Rows.Count; i++)
            {
                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["partname"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["partsize"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["partweight"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["partquantity"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["pricenoshipping"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["pricewithfedex"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["pricewithair"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["pricewithboat"].ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["pricewithocean"].ToString().ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

                cell = new PdfPCell(new Phrase(dt_part.Rows[i]["destination"].ToString().ToString(), pdffont));
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                cell.PaddingBottom = 5f;
                cell.PaddingTop = 5f;
                pdftable.AddCell(cell);

            }

            cell = new PdfPCell(new Phrase("\n"));
            cell.Colspan = 10;
            cell.HorizontalAlignment = 1;
            pdftable.AddCell(cell);

            PdfPTable pdftable2 = new PdfPTable(1);
            pdftable2.HorizontalAlignment = 1;
            PdfPCell cell2 = new PdfPCell(new Phrase("Quotation Notes:", pdffont_h));
            cell2.Colspan = 10;
            cell2.DisableBorderSide(2);
            pdftable2.AddCell(cell2);

            //读取数据库note
            string sqlnote = "select value from caculationfactor where NameInProgram=‘Note‘";
            string note = CommonMySql.ICOMold_MySql_Select_string(sqlnote);
            cell2 = new PdfPCell(new Phrase(note + "\n", pdffont));
            cell2.Colspan = 10;
            cell2.DisableBorderSide(1);
            pdftable2.AddCell(cell2);

            PdfPTable pdftable3 = new PdfPTable(1);
            pdftable3.HorizontalAlignment = 1;
            PdfPCell cell3 = new PdfPCell(new Phrase("ICOMold - Standard Terms and Conditions of Sale", pdffont_h));
            cell3.DisableBorderSide(2);
            cell3.HorizontalAlignment = 1;
            pdftable3.AddCell(cell3);

            string sqlterms = "select value from caculationfactor where NameInProgram=‘TermsandConditions‘";
            string terms = CommonMySql.ICOMold_MySql_Select_string(sqlterms);
            cell3 = new PdfPCell(new Phrase("\n\n" + terms, pdffont));
            cell3.DisableBorderSide(1);
            pdftable3.AddCell(cell3);

            document.Add(pdftable);
            //按照表格高度分页
            float tableheight = pdftable.CalculateHeights();
            if (tableheight < 600 && tableheight > 450)
            {
                document.NewPage();
            }
            document.Add(pdftable2);
            document.NewPage();
            document.Add(pdftable3);          
            document.Close();


            //pdf 下载
            FileStream filestream = new FileStream(HttpContext.Current.Server.MapPath(savePath), FileMode.Open);
            long filesize = filestream.Length;
            var bytes = new byte[(int)filesize];
            filestream.Read(bytes, 0, bytes.Length);
            filestream.Close();

            //删除生成的文件
            File.Delete(HttpContext.Current.Server.MapPath(savePath));

            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(pdfName, System.Text.Encoding.UTF8));
            HttpContext.Current.Response.AddHeader("Content-Length", filesize.ToString());
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
            HttpContext.Current.Response.Close();

        }



利用itextsharp导出pdf

标签:itextsharp   导出pdf   

原文地址:http://blog.csdn.net/adgjlxxx/article/details/43307227

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!