Wednesday, August 27, 2014

Creating pdf in C# using itext

//  first add iText dll
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Collections;
using System.Xml;
using System.Net;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    string strCon = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

    public static string customername = string.Empty;
    public static string sOrderId = string.Empty;
    public static string sCurrentStatus = string.Empty;
    public static string billingAddress = string.Empty;
    public static string billingAddress2 = string.Empty;
    public static string sPostalcode = string.Empty;
    public static string sCity = string.Empty;
    public static string sState = string.Empty;
    public static string sBookName = string.Empty;
    public static string sAddress1 = string.Empty;
    public static string sAddress2 = string.Empty;
    public static string sAddress3 = string.Empty;
    public static string sAddress4 = string.Empty;
    public static string sDeliver = string.Empty;
    public static string sSubtotal = string.Empty;
    public static string sGift = string.Empty;
    public static string sTotalPrice = string.Empty;
    public static string sDiscount = string.Empty;
    public static string sOrderDate = string.Empty;
    public static string sBookPrice = string.Empty;
    public static string smTotalPrice = string.Empty;

 
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(strCon);  
        if (!IsPostBack)
        {
           
            try
            {

                Int32 obj = 0;
                conn.Open();

                SqlCommand command = new SqlCommand();
                command.Connection = conn;
                SqlParameter param;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "usp_order_invoice";
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
                //param = new SqlParameter("@nCOId", obj1);
               // param.Direction = ParameterDirection.Input;
               // param.DbType = DbType.String;
               // command.Parameters.Add(param);

                adapter = new SqlDataAdapter(command);
                adapter.Fill(ds);

                //manipulate your data


                DataTable dt1 = ds.Tables[0];

                foreach (DataRow row in dt1.Rows)
                {
                    foreach (DataColumn col in dt1.Columns)
                   obj  = Convert.ToInt32(dt1.Rows[0]["nCOId"] );
                     LoadData(obj);
                }


        }

     

             
           

            catch (Exception ex)
            {
            }
            finally
            {
                conn.Close();
            }
        }
    }
    private void LoadData(int obj1)
    {

        SqlConnection conn = new SqlConnection(strCon);
       

        try
        {
            conn.Open();

            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            SqlParameter param;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "usp_invoiceDetail";
            SqlDataAdapter adapter;
            DataSet ds = new DataSet();
            param = new SqlParameter("@nCOId", obj1);
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            adapter = new SqlDataAdapter(command);
            adapter.Fill(ds);

            //manipulate your data

         
            DataTable dt1 = ds.Tables[0];
            DataTable dt = ds.Tables[1];

            string sessionid = HttpContext.Current.Session.SessionID.ToString();
            string pdf_name = string.Empty;
            string file_name = string.Empty;
            string invoicepdfname = string.Empty;
            string orderpdfname = string.Empty;
            int intColumnCount = 0;

            file_name = Convert.ToString(obj1);//DateTime.Now.ToString("yyyyMMddHHmmss000000zz");
            file_name = file_name.Replace("+", "");
            pdf_name = file_name + ".pdf";
         
            string sPhysicalPath = HttpContext.Current.Server.MapPath("~/upload/invoice/");   // Request.PhysicalApplicationPath + "DocProcess\\" + sessionid;
            if (!Directory.Exists(sPhysicalPath))
            {
                Directory.CreateDirectory(sPhysicalPath);
            }
           
           


            string strFileName = sPhysicalPath + pdf_name;
            iTextSharp.text.Document pdfPaymentReceipt = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 15, 25);        
            PdfWriter writerPaymentReceipt = PdfWriter.GetInstance(pdfPaymentReceipt, new FileStream(strFileName, FileMode.OpenOrCreate));
            invoicepdfname = "PaymentReceipt" + pdf_name;
            pdfPaymentReceipt.Open();


            #region header
            intColumnCount = 1;
            iTextSharp.text.Table tblOrderConfHead = new iTextSharp.text.Table(intColumnCount);
            int[] tblOrderConfHeadWidths = { 100 };
            tblOrderConfHead.SetWidths(tblOrderConfHeadWidths);
            tblOrderConfHead.WidthPercentage = 100;
            tblOrderConfHead.BorderWidth = 0.2f;
            tblOrderConfHead.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.SkyBlue);
            tblOrderConfHead.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.SkyBlue);

            iTextSharp.text.List pdfwelcomeList = new iTextSharp.text.List(false, 1);
            pdfwelcomeList.Symbol = new Chunk("");

            pdfwelcomeList.Add(new iTextSharp.text.ListItem(new iTextSharp.text.Phrase("Bookfari (US) LLC", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 13, Font.BOLD))));

            Cell headerWelcome_cell = new Cell();
            headerWelcome_cell.BorderWidth = 0f;
            headerWelcome_cell.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.Transparent);
            headerWelcome_cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
            headerWelcome_cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_BOTTOM;
            headerWelcome_cell.AddElement(pdfwelcomeList);
            tblOrderConfHead.AddCell(headerWelcome_cell);
#endregion header

         

            #region Bookfari (US) LLC
            intColumnCount = 2;
            string address2 = dt.Rows[0]["cCompNbrDesc"].ToString() + dt.Rows[0]["cCompNbr"].ToString();
            string address3 = dt.Rows[0]["cTaxNbrDesc"].ToString() + dt.Rows[0]["cTaxNbr"].ToString();
            iTextSharp.text.pdf.PdfPTable tblOrderTypeTime = new PdfPTable(intColumnCount);
            tblOrderTypeTime.SetWidths(new int[] { 30, 70 });
            tblOrderTypeTime.WidthPercentage = 100;
            tblOrderTypeTime.DefaultCell.BorderWidth = 0f;
            tblOrderTypeTime.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
            InsertCellsToTable(ref tblOrderTypeTime
                , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
                , new iTextSharp.text.Phrase(dt.Rows[0]["cMailingAddress"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));
            InsertCellsToTable(ref tblOrderTypeTime
                , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
                , new iTextSharp.text.Phrase(address2, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));

            InsertCellsToTable(ref tblOrderTypeTime
                , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI,10))
                , new iTextSharp.text.Phrase(address3, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));

       

            PdfPTable tabTemp = new PdfPTable(2);
            tabTemp.WidthPercentage = 100;                                        
            PdfPCell cellTemp = new PdfPCell();
            cellTemp.BorderWidthBottom = cellTemp.BorderWidthTop = 1f;
            cellTemp.BorderWidthLeft = cellTemp.BorderWidthRight = 0f;
            cellTemp.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.Black);

            cellTemp.AddElement(tblOrderTypeTime);
            tabTemp.AddCell(cellTemp);
     


            #endregion Bookfari (US) LLC

            #region header2
            intColumnCount = 1;
            iTextSharp.text.Table tblOrderConfHead2 = new iTextSharp.text.Table(intColumnCount);
            int[] tblOrderConfHeadWidths2 = { 100 };
            tblOrderConfHead2.SetWidths(tblOrderConfHeadWidths2);
            tblOrderConfHead2.WidthPercentage = 100;
            tblOrderConfHead2.BorderWidth = 0.2f;
            tblOrderConfHead2.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.SkyBlue);
            tblOrderConfHead2.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.SkyBlue);

            iTextSharp.text.List pdfwelcomeList2 = new iTextSharp.text.List(false, 1);
            pdfwelcomeList2.Symbol = new Chunk("");

            pdfwelcomeList2.Add(new iTextSharp.text.ListItem(new iTextSharp.text.Phrase("Invoice", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 13, Font.BOLD))));

            Cell headerWelcome_cell2 = new Cell();
            headerWelcome_cell2.BorderWidth = 0f;
            headerWelcome_cell2.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.Transparent);
            headerWelcome_cell2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
            headerWelcome_cell2.VerticalAlignment = iTextSharp.text.Element.ALIGN_BOTTOM;
            headerWelcome_cell2.AddElement(pdfwelcomeList2);
            tblOrderConfHead2.AddCell(headerWelcome_cell2);
            #endregion header2
            #region invoice

            PdfPTable tabDlvryBillDetail;
            PdfPCell tabDlvryBillDetailCell;
            tabDlvryBillDetail = new PdfPTable(4);
            tabDlvryBillDetail.WidthPercentage = 100;
            tabDlvryBillDetail.DefaultCell.BorderWidth = 0f;
            tabDlvryBillDetail.DefaultCell.BorderColorBottom = new iTextSharp.text.Color(System.Drawing.Color.Black);
         
            InsertCellsToTable(ref tabDlvryBillDetail
            , new iTextSharp.text.Phrase("Invoice", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
            , new iTextSharp.text.Phrase(dt.Rows[0]["cAddr1"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
            , new iTextSharp.text.Phrase("Invoice Number:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
            , new iTextSharp.text.Phrase(dt.Rows[0]["nCOId"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
            );

            InsertCellsToTable(ref tabDlvryBillDetail
           , new iTextSharp.text.Phrase("To:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase(dt.Rows[0]["cAddr2"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase("Invoice Date:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase(dt.Rows[0]["dOrderDate"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           );

            InsertCellsToTable(ref tabDlvryBillDetail
           , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase(dt.Rows[0]["SubUrnan"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase("Customer Code:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase(dt.Rows[0]["nCompId"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           );


            InsertCellsToTable(ref tabDlvryBillDetail
           , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase(dt.Rows[0]["cCountryCode"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase("Cust. OrderNo:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           , new iTextSharp.text.Phrase(dt.Rows[0]["cCompOrderNo"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
           );

            pdfPaymentReceipt.Add(tblOrderConfHead);
            pdfPaymentReceipt.Add(tblOrderTypeTime);          
            pdfPaymentReceipt.Add(tblOrderConfHead2);
            pdfPaymentReceipt.Add(tabDlvryBillDetail);
           


         
            #endregion invoice

            # region itemdetail

            PdfPTable tblItemsnew = new PdfPTable(6);
            tblItemsnew.SpacingBefore = 30;
            tblItemsnew.SpacingAfter = 20;
            tblItemsnew.DefaultCell.Padding = 1;
            float[] headerwidths_items_new = {2,7, 20, 6, 5, 5 };
            tblItemsnew.SetWidths(headerwidths_items_new);
            tblItemsnew.WidthPercentage = 100;
            tblItemsnew.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
            tblItemsnew.DefaultCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
            //tblItemsnew.DefaultCell.BorderWidth = 0.001f;
            tblItemsnew.DefaultCell.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
            tblItemsnew.HeaderRows = 0;

            // PdfPTable table = new PdfPTable(3); "TIMES_ROMAN", BaseFont.WINANSI, 7
           
            //PdfPCell cell = new PdfPCell(new Phrase("Qty", new Font(Font.TIMES_ROMAN, 8f, Font.NORMAL, Color.BLACK)));
            //cell.BackgroundColor = new Color(System.Drawing.Color.SkyBlue);
            //cell.BorderColor = new Color(255, 242, 0);
            //cell.Border = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;
            //cell.BorderWidthBottom = 3f;
            //cell.BorderWidthTop = 3f;
            //cell.PaddingBottom = 10f;
            //cell.PaddingLeft = 20f;
            //cell.PaddingTop = 4f;
            //tblItemsnew.AddCell(cell);
            //PdfPCell cell1 = new PdfPCell(new Phrase("ISBN13", new Font(Font.TIMES_ROMAN, 8f, Font.NORMAL, Color.BLACK)));
            //cell1.BackgroundColor = new Color(System.Drawing.Color.SkyBlue);
            //cell1.BorderColor = new Color(255, 242, 0);
            //cell1.Border = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;
            //cell1.BorderWidthBottom = 3f;
            //cell1.BorderWidthTop = 3f;
            //cell1.PaddingBottom = 10f;
            //cell1.PaddingLeft = 20f;
            //cell1.PaddingTop = 4f;
            //tblItemsnew.AddCell(cell1);
            //tblItemsnew.AddCell("ISBN13");
            //tblItemsnew.AddCell("Title");
            //tblItemsnew.AddCell("Condition");
            //tblItemsnew.AddCell("USD Sale Price");
            //tblItemsnew.AddCell("USD Ext Price");




            InsertCellsToTable(ref tblItemsnew
                , new iTextSharp.text.Phrase("Qty", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                , new iTextSharp.text.Phrase("ISBN13", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                , new iTextSharp.text.Phrase("Title", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                , new iTextSharp.text.Phrase("Condition", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                , new iTextSharp.text.Phrase("USD Sale Price", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                , new iTextSharp.text.Phrase("USD Ext Price", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7)));

           


            int i = 0;
            int sQty = 0;
       
            string total = string.Empty;
            string currency = string.Empty;
            for (i = 0; i <= dt1.Rows.Count - 1; i++)
            {
                Int32 check= Convert.ToInt32(dt1.Rows[i]["QtytoAccept"]);
                if (check > 0)
                {
                    decimal USDExtSale = 0;
                    sBookName = dt1.Rows[i]["cTitle"].ToString();
                    sQty = Convert.ToInt32(dt1.Rows[i]["QtytoAccept"]) == 0 ? 1 : Convert.ToInt32(dt1.Rows[i]["QtytoAccept"]);
                    decimal newprice = Convert.ToDecimal(dt1.Rows[i]["NewPrice"]);
                    currency = dt1.Rows[i]["cCurrDispNm"].ToString();


                    USDExtSale = sQty * newprice;
                    total = currency + USDExtSale.ToString();

                    InsertCellsToTable(ref tblItemsnew
                   , new iTextSharp.text.Phrase(sQty == 0 ? string.Empty : sQty.ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(dt1.Rows[i]["cISBN13"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(sBookName, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(dt1.Rows[i]["cCondition"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(dt1.Rows[i]["NewPrice"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(USDExtSale.ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7)));
                }

                else
                {
                    decimal USDExtSale = 0;
                    sBookName = dt1.Rows[i]["cTitle"].ToString();
                    sQty = Convert.ToInt32(dt1.Rows[i]["QtytoAccept"]) == 0 ? 1 : Convert.ToInt32(dt1.Rows[i]["QtytoAccept"]);
                    decimal newprice = Convert.ToDecimal(dt1.Rows[i]["NewPrice"]);
                    currency = dt1.Rows[i]["cCurrDispNm"].ToString();
                   
                    InsertCellsToTable(ref tblItemsnew
                   , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(sBookName, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(dt1.Rows[i]["cCondition"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(dt1.Rows[i]["NewPrice"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
                   , new iTextSharp.text.Phrase(USDExtSale.ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7)));
                }

            }

            InsertCellsToTable(ref tblItemsnew
           , new iTextSharp.text.Phrase(sQty == 0 ? string.Empty : sQty.ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
           , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
           , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
           , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
           , new iTextSharp.text.Phrase("Total: ", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 8, iTextSharp.text.Font.BOLD))
           , new iTextSharp.text.Phrase(total, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7)));

            InsertCellsToTable(ref tblItemsnew
        , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
        , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
        , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
        , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7))
        , new iTextSharp.text.Phrase("Tax Amount:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 8, iTextSharp.text.Font.BOLD))
        , new iTextSharp.text.Phrase(currency + "0.00", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 7)));

#endregion itemdetail

            #region bottomblock

            intColumnCount = 2;
            iTextSharp.text.pdf.PdfPTable tblOrderTypeTime1 = new PdfPTable(intColumnCount);
            tblOrderTypeTime1.SetWidths(new int[] { 40, 70 });
            tblOrderTypeTime1.WidthPercentage = 100;
            tblOrderTypeTime1.DefaultCell.BorderWidth = 0f;
            tblOrderTypeTime1.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
            InsertCellsToTable(ref tblOrderTypeTime1
                , new iTextSharp.text.Phrase(dt.Rows[0]["cPayInstructions"].ToString() + ":", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
                , new iTextSharp.text.Phrase(string.Empty, iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));
            InsertCellsToTable(ref tblOrderTypeTime1
                , new iTextSharp.text.Phrase("Bank:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
                , new iTextSharp.text.Phrase(dt.Rows[0]["cBank"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));

            InsertCellsToTable(ref tblOrderTypeTime1
                , new iTextSharp.text.Phrase("Branch:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI,10))
                , new iTextSharp.text.Phrase(dt.Rows[0]["cBranch"].ToString() + dt.Rows[0]["cBranchAddress"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));

            InsertCellsToTable(ref tblOrderTypeTime1
                 , new iTextSharp.text.Phrase(dt.Rows[0]["cRefCodeNm"].ToString() + ":", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
                 , new iTextSharp.text.Phrase(dt.Rows[0]["cRefCode"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));

            InsertCellsToTable(ref tblOrderTypeTime1
              , new iTextSharp.text.Phrase("Account Name:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
              , new iTextSharp.text.Phrase(dt.Rows[0]["cAccountNm"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));


            InsertCellsToTable(ref tblOrderTypeTime1
              , new iTextSharp.text.Phrase("Account Number:", iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10))
              , new iTextSharp.text.Phrase(dt.Rows[0]["cAccountNo"].ToString(), iTextSharp.text.FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 10)));

            PdfPTable tabTemp1 = new PdfPTable(2);
            tabTemp1.WidthPercentage = 100;                                        
            PdfPCell cellTemp1 = new PdfPCell();
            cellTemp1.BorderWidthBottom = cellTemp.BorderWidthTop = 1f;
            cellTemp1.BorderWidthLeft = cellTemp.BorderWidthRight = 0f;
            cellTemp1.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.Black);

            cellTemp.AddElement(tblOrderTypeTime1);
            tabTemp.AddCell(cellTemp1);      
           
            #endregion bottomblock        
           
            pdfPaymentReceipt.Add(tblItemsnew);
            pdfPaymentReceipt.Add(tblOrderTypeTime1);      
            pdfPaymentReceipt.Close();
            //Response.Clear();
            //Response.AppendHeader("content-disposition", "attachment; filename=" + pdf_name);
            //Response.ContentType = "text/plain";
            //Response.WriteFile(Request.PhysicalApplicationPath + "/DocProcess/" + sessionid + "/" + pdf_name);

            //Response.Flush();

            //if (Directory.Exists(sPhysicalPath))
            //{
            //    System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(sPhysicalPath);
            //    foreach (FileInfo file in downloadedMessageInfo.GetFiles())
            //    {
            //   //     file.Delete();
            //    }
            //    Directory.Delete(sPhysicalPath);

            //}

           // Response.End();
        }









        catch (Exception e)
        {
            Console.WriteLine("Error: " + e);
        }
        finally
        {
            conn.Close();
        }






    }

    #region InsertCellsToTable
    protected static void InsertCellsToTable(ref PdfPTable tbl, params iTextSharp.text.Phrase[] cells)
    {
        try
        {
            for (int i = 0; i < cells.Length; i++)
            {
                PdfPCell new_cell = new PdfPCell();              
                new_cell.BorderWidth = tbl.DefaultCell.BorderWidth;
                new_cell.BorderColor = tbl.DefaultCell.BorderColor;
                new_cell.BorderColor = tbl.DefaultCell.BackgroundColor;
                new_cell.BorderColorBottom = tbl.DefaultCell.BorderColorBottom;
                new_cell.HorizontalAlignment = tbl.DefaultCell.HorizontalAlignment;
                new_cell.Phrase = cells[i];
                tbl.AddCell(new_cell);
            }
        }
        catch (Exception ex)
        {
         
        }
    }
    #endregion
}
   

No comments:

Post a Comment