导出pdflabel

  private void OutContactLabelPdf(DataTable Data, string filename, string FontPath, float FontSize)
        {
            MemoryStream m = new MemoryStream();
            Document document = new Document(PageSize.LETTER);
            document.SetMargins(float.Parse("13.68"), float.Parse("13.68"), float.Parse("36"), float.Parse("36"));
            try
            {
                HttpContext.Current.Response.ContentType = "application/pdf";
                PdfWriter.GetInstance(document, m);

 

                document.Open();
                BaseFont baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                BaseFont arial = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

                // iTextSharp.text.Font font = new iTextSharp.text.Font(arial, FontSize);
                iTextSharp.text.Font font = new iTextSharp.text.Font(arial, 9);
                iTextSharp.text.Font fonttitle = new iTextSharp.text.Font(arial, FontSize + 3, Font.BOLD);

                int PerPageDataCount = 30;

                int CountFlag = Data.Rows.Count;

                int dataindex = 0;

                while (CountFlag > 0)
                {
                    #region WriteContactPDF // add by Nic Rom2 2010-06-25

                    iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(3);
                    table.WidthPercentage = float.Parse("100");


                    for (int i = 0; i < PerPageDataCount; i++)
                    {
                        if (CountFlag == 0)
                        {
                            break;
                        }

                        iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Paragraph(string.Format("{0}   {1}\n{2}\n{3}{4}   {5}   {6}", Data.Rows[dataindex]["FirstName"].ToString(), Data.Rows[dataindex]["LastName"].ToString(), Data.Rows[dataindex]["Address1"].ToString(), Data.Rows[dataindex]["Address2"].ToString() == "" ? Data.Rows[dataindex]["Address2"].ToString() : Data.Rows[dataindex]["Address2"].ToString() + "\n", Data.Rows[dataindex]["City"].ToString(), Data.Rows[dataindex]["State"].ToString(), Data.Rows[dataindex]["ZipCode"].ToString()), font));
                        cell.MinimumHeight = float.Parse("72");
                        // cell.Padding = float.Parse("10");
                        int temp = i + 1;
                        int result = temp % 3;

                        if (result == 1)
                        {
                            cell.PaddingLeft = float.Parse("10");
                            cell.PaddingRight = float.Parse("14.5");
                            cell.PaddingBottom = float.Parse("10");
                            cell.PaddingTop = float.Parse("10");
                        }
                        if (result == 2)
                        {
                            cell.PaddingLeft = float.Parse("14.5");
                            cell.PaddingRight = float.Parse("14.5");
                            cell.PaddingBottom = float.Parse("10");
                            cell.PaddingTop = float.Parse("10");
                        }
                        if (result == 0)
                        {
                            cell.PaddingLeft = float.Parse("14.5");
                            cell.PaddingRight = float.Parse("10");
                            cell.PaddingBottom = float.Parse("10");
                            cell.PaddingTop = float.Parse("10");
                        }


                        cell.DisableBorderSide(1);
                        cell.DisableBorderSide(2);
                        cell.DisableBorderSide(4);
                        cell.DisableBorderSide(8);
                        table.AddCell(cell);

                        CountFlag--;
                        dataindex++;
                    }
                    table.CompleteRow();

                    document.Add(table);
                    document.NewPage();

                    #endregion
                }

 


                document.Close();
            }
            catch (DocumentException ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("Attachment;fileName={0}", filename));
            HttpContext.Current.Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
            HttpContext.Current.Response.OutputStream.Flush();
            HttpContext.Current.Response.OutputStream.Close();
            HttpContext.Current.Response.End();
            HttpContext.Current.Response.Close();
        }
        private void OutContactLabelPdf(DataTable datatable, string PDFFilePath)
        {
            OutContactLabelPdf(datatable, PDFFilePath, HttpContext.Current.Server.MapPath(".") + "\\Images\\simsun.ttc,1", 13);//./Images/arial.ttf,1   "c:\\winnt\\FONTS\\simsun.ttc,1"
        }
        private DataTable GenerateExcelDataTable()
        {
            TContact contact = new TContact();
            ReturnValue _result = new ReturnValue();
            GetContactList();
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName", System.Type.GetType("System.String"));
            dt.Columns.Add("LastName", System.Type.GetType("System.String"));
            dt.Columns.Add("Address1", System.Type.GetType("System.String"));
            dt.Columns.Add("Address2", System.Type.GetType("System.String"));
            dt.Columns.Add("City", System.Type.GetType("System.String"));
            dt.Columns.Add("State", System.Type.GetType("System.String"));
            dt.Columns.Add("ZipCode", System.Type.GetType("System.String"));
            dt.Columns.Add("Email", System.Type.GetType("System.String"));
            dt.Columns.Add("Phone", System.Type.GetType("System.String"));

 

            foreach (string id in ConsultantList)
            {
                _result = contact.getContactById(Convert.ToInt32(id));
                if (_result.Success == false)
                {
                    FrameWorkV3.BLL.Common.ProcessError(_result);
                    return dt;
                }

                contact = _result.Object as TContact;
                DataRow dr = dt.NewRow();
                dr["FirstName"] = contact.FirstName;
                dr["LastName"] = contact.LastName;
                dr["Address1"] = contact.Address1;
                dr["Address2"] = contact.Address2;
                dr["City"] = contact.City;
                dr["State"] = contact.State;
                dr["ZipCode"] = contact.PostalCode;
                dr["Email"] = contact.Email;            
                dr["Phone"] = FormatPhone(contact.Phone1);

                dt.Rows.Add(dr);

            }
       
            return dt;
         
        }
        private void OutputContactExcel(string strFileName, DataTable dtOutPut, string strTextType)
        {
            StringWriter sw = new StringWriter();

            string strColumns = "";

            foreach (DataColumn Columns in dtOutPut.Columns)
            {
                strColumns = strColumns + "\t" + Columns.ColumnName;
            }
            sw.WriteLine(strColumns.Substring(1));

            foreach (DataRow dr in dtOutPut.Rows)
            {
                strColumns = "";
                foreach (DataColumn Columns in dtOutPut.Columns)
                {
                    strColumns = strColumns + "\t" + dr[Columns.ColumnName];
                }
                sw.WriteLine(strColumns.Substring(1));
            }

            sw.Close();
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";

            if (strTextType != "")
            {
                System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(strTextType); //"GB2312"
            }
            System.Web.HttpContext.Current.Response.Write(sw);
            System.Web.HttpContext.Current.Response.End();
        }
        private DataTable GeneratePrintDataTable()
        {
            TContact contact = new TContact();
            ReturnValue _result = new ReturnValue();
            GetContactList();
            DataTable dt = new DataTable();
           
            dt.Columns.Add("LastName", System.Type.GetType("System.String"));
            dt.Columns.Add("FirstName", System.Type.GetType("System.String"));
            dt.Columns.Add("Address1", System.Type.GetType("System.String"));
            dt.Columns.Add("Address2", System.Type.GetType("System.String"));
            dt.Columns.Add("City", System.Type.GetType("System.String"));
            dt.Columns.Add("State", System.Type.GetType("System.String"));
            dt.Columns.Add("ZipCode", System.Type.GetType("System.String"));
            dt.Columns.Add("Phone", System.Type.GetType("System.String"));
            dt.Columns.Add("E-Mail", System.Type.GetType("System.String"));


            foreach (string id in ConsultantList)
            {
                _result = contact.getContactById(Convert.ToInt32(id));
                if (_result.Success == false)
                {
                    FrameWorkV3.BLL.Common.ProcessError(_result);
                    return dt;
                }

                contact = _result.Object as TContact;
                DataRow dr = dt.NewRow();
                dr["FirstName"] = contact.FirstName;
                dr["LastName"] = contact.LastName;
                dr["Address1"] = contact.Address1;
                dr["Address2"] = contact.Address2;
                dr["City"] = contact.City;
                dr["State"] = contact.State;
                dr["ZipCode"] = contact.PostalCode;
                dr["Phone"] = FormatPhone(contact.Phone1);
                dr["E-Mail"] = contact.Email;
                dt.Rows.Add(dr);

            }


            return dt;

        }
        private void PrintContact(DataTable Data, string filename, string FontPath, float FontSize)
        {
            MemoryStream m = new MemoryStream();
            Document document = new Document();

            try
            {
                HttpContext.Current.Response.ContentType = "application/pdf";
                PdfWriter.GetInstance(document, m);

                document.Open();
                BaseFont baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

                iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, FontSize);
                iTextSharp.text.Font fonttitle = new iTextSharp.text.Font(baseFont, FontSize + 3, Font.BOLD);
                PdfPTable table = new PdfPTable(Data.Columns.Count);
                for (int j = 0; j < Data.Columns.Count; j++)
                {
                    table.AddCell(new Phrase(Data.Columns[j].ColumnName, fonttitle));
                }

                for (int i = 0; i < Data.Rows.Count; i++)
                {

                    for (int j = 0; j < Data.Columns.Count; j++)
                    {
                        table.AddCell(new Phrase(Data.Rows[i][j].ToString(), font));
                    }
                }
                table.WidthPercentage = float.Parse("100");
                document.Add(table);
                document.Close();
            }
            catch (DocumentException ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("Attachment;fileName={0}", filename));
            HttpContext.Current.Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
            HttpContext.Current.Response.OutputStream.Flush();
            HttpContext.Current.Response.OutputStream.Close();
            HttpContext.Current.Response.End();
            HttpContext.Current.Response.Close();


        }
       #endregion
        //*************************************
        protected bool ValidationContactInformation(TContact _contact)
        {
            bool IsSuccess = false;

            if (string.IsNullOrEmpty(_contact.FirstName))
            {
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(_contact.LastName))
            {
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(_contact.Email))
            {
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(_contact.Phone1))
            {
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(_contact.Address1))
            {
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(_contact.PostalCode))
            {
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(_contact.City))
            {
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(_contact.State))
            {
                return IsSuccess;
            }

            IsSuccess = true;
            return IsSuccess;
        }

        protected void GetAllContactList()
        {
            List<string> allcustomerlist = this.AllConsultantList;

            foreach (GridViewRow Row in gv_ContactList.Rows)
            {
                CheckBox cbox_item = Row.FindControl("cbox_One") as CheckBox;
                if (cbox_item != null)
                {
                    HiddenField hf_Field = Row.FindControl("hf_FieldOne") as HiddenField;
                    if (hf_Field != null)
                    {
                        if (cbox_item.Enabled)
                        {
                            if (allcustomerlist.Contains(hf_Field.Value) == false)
                            {
                                allcustomerlist.Add(hf_Field.Value);
                            }
                        }
                    }
                }
            }

            this.AllConsultantList = allcustomerlist;
        }
        //add by Nic Rom2 2010-06-28
        protected void GetContactList()
        {
            List<string> customerlist = this.ConsultantList;

            foreach (GridViewRow Row in gv_ContactList.Rows)
            {
                CheckBox cbox_item = Row.FindControl("cbox_One") as CheckBox;
                if (cbox_item != null)
                {
                    HiddenField hf_Field = Row.FindControl("hf_FieldOne") as HiddenField;
                    if (hf_Field != null)
                    {
                        if (cbox_item.Checked)
                        {
                            if (customerlist.Contains(hf_Field.Value) == false)
                            {
                                customerlist.Add(hf_Field.Value);
                            }
                        }
                        else
                        {
                            if (customerlist.Contains(hf_Field.Value) == true)
                            {
                                customerlist.Remove(hf_Field.Value);
                            }
                        }
                    }
                }
            }

            this.ConsultantList = customerlist;
        }
        private int BindCheckedContact()
        {
            int i = 0;
            List<string> customerlist = this.ConsultantList;
            foreach (GridViewRow Row in gv_ContactList.Rows)
            {
                CheckBox cbox_item = Row.FindControl("cbox_One") as CheckBox;
                if (cbox_item != null)
                {
                    HiddenField hf_Field = Row.FindControl("hf_FieldOne") as HiddenField;
                    if (hf_Field != null)
                    {
                        cbox_item.Checked = customerlist.Contains(hf_Field.Value);
                        if (cbox_item.Checked)
                        {
                            i++;
                        }
                    }
                }
            }
            return i;
        }

 

 

    private void OutContactLabelPdf(DataTable datatable, string PDFFilePath)
        {
            OutContactLabelPdf(datatable, PDFFilePath, HttpContext.Current.Server.MapPath(".") + "\\Images\\simsun.ttc,1", 13);//./Images/arial.ttf,1   "c:\\winnt\\FONTS\\simsun.ttc,1"
        }

posted @ 2010-07-05 17:14  Devil_Nic  阅读(461)  评论(0编辑  收藏  举报