コード例 #1
0
        public void GetPDF()
        {
            string filename = string.Empty;
            string jsonStr = String.Empty;
            try
            {
                if (!string.IsNullOrEmpty(Request.Params["ticket_id"]))
                {
                    filename = "picking_detail" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                    string newPDFName = Server.MapPath(excelPath) + filename;
                    if (System.IO.File.Exists(newPDFName))
                    {
                        //設置文件的屬性,以防刪除文件的時候因為文件的屬性造成無法刪除
                        System.IO.File.SetAttributes(newPDFName, FileAttributes.Normal);
                        System.IO.File.Delete(newPDFName);
                    }
                    TicketQuery query = new TicketQuery();
                    string temp = Request.Params["ticket_id"].ToString();
                    temp = temp.Remove(temp.LastIndexOf(','));
                    query.ticketIds = temp;
                    _tkMgr = new TicketMgr(mySqlConnectionString);
                    List<TicketQuery> store = _tkMgr.GetPickingDetail(query);
                    List<TicketQuery> FMode = new List<TicketQuery>();
                    List<TicketQuery> CMode = new List<TicketQuery>();
                    foreach (var item in store)
                    {
                        if (item.combined_mode > 1)
                        {

                            if (item.item_mode == 1)
                            {
                                FMode.Add(item);
                            }
                            else
                            {
                                CMode.Add(item);
                            }
                        }
                        else
                        {
                            CMode.Add(item);
                        }

                    }
                    foreach (var fitem in FMode)
                    {
                        foreach (var citem in CMode)
                        {
                            if (fitem.parent_id == citem.parent_id && fitem.pack_id == citem.pack_id)
                            {
                                citem.buy_num = citem.buy_num * citem.parent_num;
                            }
                        }
                    }
                    string[] strId = query.ticketIds.ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string str = strId[strId.Length - 1].ToString();
                    int de = 8 - str.Length;
                    for (int z = 0; z < de; z++)
                    {
                        str = str.Insert(0, "0");
                    }
                    str = str.Insert(0, "P");
                    BarCode.Code128 _Code = new BarCode.Code128();
                    _Code.ValueFont = new System.Drawing.Font("宋体", 20);
                    System.Drawing.Bitmap imgTemp = _Code.GetCodeImage(str, BarCode.Code128.Encode.Code128A);
                    imgTemp.Save(System.AppDomain.CurrentDomain.BaseDirectory + "\\ImportUserIOExcel\\" + "BarCode.gif", System.Drawing.Imaging.ImageFormat.Gif);
                    BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                    iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 14, iTextSharp.text.Font.UNDERLINE, iTextSharp.text.BaseColor.RED);
                    iTextSharp.text.Font font = new iTextSharp.text.Font(bfChinese, 8, iTextSharp.text.Font.GetStyleValue("font-size:xx-small"), new iTextSharp.text.BaseColor(0, 0, 0));//黑
                    iTextSharp.text.Font bigFont = new iTextSharp.text.Font(bfChinese, 10, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0));//黑
                    Document document = new Document(PageSize.A4.Rotate(), (float)5, (float)5, (float)5, (float)0.5);

                    PdfWriter.GetInstance(document, new FileStream(newPDFName, FileMode.OpenOrCreate));

                    document.Open();
                    Phrase ph = new Phrase("\n ", font);
                    iTextSharp.text.Image IMG = iTextSharp.text.Image.GetInstance(Server.MapPath("../ImportUserIOExcel/BarCode.gif"));
                    IMG.ScaleToFit(100, 30);
                    Chunk ck = new Chunk(IMG, 580, -30); //图片可设置 偏移
                    ph.Add(ck);
                    document.Add(ph);
                    Phrase p1 = new Phrase("\n \n", font);
                    document.Add(p1);
                    PdfPTable table = new PdfPTable(7);
                    //table.BorderWidth = 0;
                    //table.Cellpadding = 3;
                    //table.Cellspacing = 3;
                    table.SpacingBefore = 5;
                    table.SetWidths(new int[] { 20, 30, 20, 40, 10, 15, 15 });
                    PdfPCell cell = new PdfPCell(new Phrase("付款單號", font));
                    cell.BorderWidthLeft = 0;
                    cell.BorderWidthBottom = 0.5f;
                    cell.BorderWidthRight = 0;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase("付款日期", font));
                    cell.BorderWidthLeft = 0;
                    cell.BorderWidthBottom = 0.5f;
                    cell.BorderWidthRight = 0;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase("供應商", font));
                    cell.BorderWidthLeft = 0;
                    cell.BorderWidthBottom = 0.5f;
                    cell.BorderWidthRight = 0;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase("商品名稱", font));
                    cell.BorderWidthLeft = 0;
                    cell.BorderWidthBottom = 0.5f;
                    cell.BorderWidthRight = 0;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase("數量", font));
                    cell.BorderWidthLeft = 0;
                    cell.BorderWidthBottom = 0.5f;
                    cell.BorderWidthRight = 0;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase("收件人", font));
                    cell.BorderWidthLeft = 0;
                    cell.BorderWidthBottom = 0.5f;
                    cell.BorderWidthRight = 0;
                    table.AddCell(cell);
                    cell = new PdfPCell(new Phrase("訂購人", font));
                    cell.BorderWidthLeft = 0;
                    cell.BorderWidthBottom = 0.5f;
                    cell.BorderWidthRight = 0;
                    table.AddCell(cell);
                    for (int i = 0; i < CMode.Count; i++)
                    {
                        if (i == 0)
                        {
                            cell = new PdfPCell(new Phrase(CMode[i].order_id.ToString(), font));
                            cell.BorderWidthLeft = 0;
                            cell.BorderWidthRight = 0;
                            cell.BorderWidthTop = 0;
                            cell.BorderWidthBottom = 0;
                            table.AddCell(cell);
                            cell = new PdfPCell(new Phrase(CommonFunction.GetNetTime(CMode[i].order_date_pay).ToString("yyyy-MM-dd HH:mm:ss"), font));
                            cell.BorderWidthLeft = 0;
                            cell.BorderWidthRight = 0;
                            cell.BorderWidthTop = 0;
                            cell.BorderWidthBottom = 0;
                            table.AddCell(cell);
                            cell = new PdfPCell(new Phrase(CMode[i].vendor_name_simple.ToString(), font));
                            cell.BorderWidthLeft = 0;
                            cell.BorderWidthRight = 0;
                            cell.BorderWidthTop = 0;
                            cell.BorderWidthBottom = 0;
                            table.AddCell(cell);
                            cell = new PdfPCell(new Phrase(CMode[i].product_name.ToString() + CMode[i].spec_name.ToString() + CMode[i].spec_name1.ToString(), font));
                            cell.BorderWidthLeft = 0;
                            cell.BorderWidthRight = 0;
                            cell.BorderWidthTop = 0;
                            cell.BorderWidthBottom = 0;
                            table.AddCell(cell);
                            cell = new PdfPCell(new Phrase(CMode[i].buy_num.ToString(), font));
                            cell.BorderWidthLeft = 0;
                            cell.BorderWidthRight = 0;
                            cell.BorderWidthTop = 0;
                            cell.BorderWidthBottom = 0;
                            table.AddCell(cell);
                            cell = new PdfPCell(new Phrase(CMode[i].delivery_name.ToString(), font));
                            cell.BorderWidthLeft = 0;
                            cell.BorderWidthRight = 0;
                            cell.BorderWidthTop = 0;
                            cell.BorderWidthBottom = 0;
                            table.AddCell(cell);
                            cell = new PdfPCell(new Phrase(CMode[i].order_name.ToString(), font));
                            cell.BorderWidthLeft = 0;
                            cell.BorderWidthRight = 0;
                            cell.BorderWidthTop = 0;
                            cell.BorderWidthBottom = 0;
                            table.AddCell(cell);
                        }
                        else
                        {
                            if (CMode[i].order_id == CMode[i - 1].order_id)
                            {
                                cell = new PdfPCell(new Phrase(CMode[i].order_id.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CommonFunction.GetNetTime(CMode[i].order_date_pay).ToString("yyyy-MM-dd HH:mm:ss"), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].vendor_name_simple.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].product_name.ToString() + CMode[i].spec_name.ToString() + CMode[i].spec_name1.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].buy_num.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].delivery_name.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].order_name.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                            }
                            else
                            {
                                cell = new PdfPCell(new Phrase(CMode[i].order_id.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0.5f;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CommonFunction.GetNetTime(CMode[i].order_date_pay).ToString("yyyy-MM-dd HH:mm:ss"), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0.5f;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].vendor_name_simple.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0.5f;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].product_name.ToString() + CMode[i].spec_name.ToString() + CMode[i].spec_name1.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0.5f;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].buy_num.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0.5f;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].delivery_name.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0.5f;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                                cell = new PdfPCell(new Phrase(CMode[i].order_name.ToString(), font));
                                cell.BorderWidthLeft = 0;
                                cell.BorderWidthRight = 0;
                                cell.BorderWidthTop = 0.5f;
                                cell.BorderWidthBottom = 0;
                                table.AddCell(cell);
                            }
                        }
                    }
                    document.Add(table);
                    document.Close();
                    Response.Clear();
                    Response.Charset = "gb2312";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    // Response.AddHeader("Content-Disposition", "attach-ment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".pdf ");
                    Response.AddHeader("Content-Disposition", "attach-ment;filename=" + filename);
                    Response.WriteFile(newPDFName);
                }
            }
            catch (Exception ex)
            {
                Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
                logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
                logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                log.Error(logMessage);
                jsonStr = "{success:false,result:0}";
            }
        }