Saturday, March 17, 2012

Bill PDF C#

using System;
//using System.Windows.Forms;
//using System.Drawing;
//using System.Drawing.Printing;
using System.Collections;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace TyroDeveloperDLL
{
    public class BillPDF
    {
        public BillPDF()
        {
            
            myBill.AddAuthor("TyroDeveloper");
            myBill.AddCreator("JUAN GABRIEL CASTILLO TURRUBIATES");
            myBill.AddTitle("Ticket de Venta");
        }

        PdfWriter writer = null;
        PdfContentByte cb = null;

        ArrayList headerLines = new ArrayList();
        ArrayList subHeaderLines = new ArrayList();
        ArrayList items = new ArrayList();
        ArrayList totales = new ArrayList();
        ArrayList footerLines = new ArrayList();
        private string headerImage = "";
        bool _DrawItemHeaders = true;
        int startItemsOn = 20;
        string path = "";
        string file_name = "";
        //int maxChar = 100;
        int maxCharDescription = 50;

        int imageHeight = 0;

        float idProductX = 0;
        float descProductX = 0;
        float amountProductX = 0;
        float qtyProductX = 0;
        float priceProductX = 0;
        float topMargin = 5;
        
        static int fontSize = 12;
        static BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
        static Font font = new Font(bfCourier, fontSize, Font.NORMAL, Color.BLACK);

        Document myBill = new Document(PageSize.LETTER, 0, 0, 0, 0); //Aqui se ponen todos los objetos

        string line = "";

        #region Properties
        
        public Int32 StartItemsOn { get { return startItemsOn; } set { startItemsOn = value; } }
        public float IdProductX { get { return idProductX; } set { idProductX = value; } }
        public float DescProductX { get { return descProductX; } set { descProductX = value; } }
        public float AmountProductX { get { return amountProductX; } set { amountProductX = value; } }
        public float QtyProductX { get { return qtyProductX; } set { qtyProductX = value; } }
        public float PriceProductX { get { return priceProductX; } set { priceProductX = value; } }
        public String Path
        {
            get { return path; }
            set { path = value; }
        }

        public String FileName
        {
            get { return file_name; }
            set { file_name = value; }
        }

        public String FullFileName
        {
            get { return (String.Format("{0}{1}", path, file_name)); }
        }
        public String HeaderImage
        {
            get { return headerImage; }
            set { if (headerImage != value) headerImage = value; }
        }

       

        public bool DrawItemHeaders
        {
            set { _DrawItemHeaders = value; }
        }

        public int MaxCharDescription
        {
            get { return maxCharDescription; }
            set { if (value != maxCharDescription) maxCharDescription = value; }
        }

        public int FontSize
        {
            get { return fontSize; }
            set { if (value != fontSize) fontSize = value; }
        }

        public Font FontName
        {
            get { return font; }
            set { if (value != font) font = value; }
        }
        //CustomerName
        string customerName = "";
        public String CustomerName {get { return customerName; }set { customerName = value; }}
        float customerNameX = 0;
        public float CustomerNameX { get { return customerNameX; } set { customerNameX = value; }}
        float customerNameY = 0;
        public float CustomerNameY {get { return customerNameY; }set { customerNameY = value; }}
        //BillID
        string billID = "";
        public String BillID {get { return billID; } set { billID = value; } }
        float billIDX = 0;
        public float BillIDX { get { return billIDX; } set { billIDX = value; } }
        float billIDY = 0;
        public float BillIDY { get { return billIDY; } set { billIDY = value; } }
        //BillDate
        string billDate = "";
        public String BillDate { get { return billDate; } set { billDate = value; } }
        float billDateX = 0;
        public float BillDateX { get { return billDateX; } set { billDateX = value; } }
        float billDateY = 0;
        public float BillDateY { get { return billDateY; } set { billDateY = value; } }
        //CustomerAddress
        string customerAddress = "";
        public String CustomerAddress { get { return customerAddress; } set { customerAddress = value; } }
        float customerAddressX = 0;
        public float CustomerAddressX { get { return customerAddressX; } set { customerAddressX = value; } }
        float customerAddressY = 0;
        public float CustomerAddressY { get { return customerAddressY; } set { customerAddressY = value; } }
        //CustomerCity
        string customerCity = "";
        public String CustomerCity { get { return customerCity; } set { customerCity = value; } }
        float customerCityX = 0;
        public float CustomerCityX { get { return customerCityX; } set { customerCityX = value; } }
        float customerCityY = 0;
        public float CustomerCityY { get { return customerCityY; } set { customerCityY = value; } }
        //CustomerRFC
        string customerRFC = "";
        public String CustomerRFC { get { return customerRFC; } set { customerRFC = value; } }
        float customerRFCX = 0;
        public float CustomerRFCX { get { return customerRFCX; } set { customerRFCX = value; } }
        float customerRFCY = 0;
        public float CustomerRFCY { get { return customerRFCY; } set { customerRFCY = value; } }
        //CustomerPostalCode
        string customerPostalCode = "";
        public String CustomerPostalCode { get { return customerPostalCode; } set { customerPostalCode = value; } }
        float customerPostalCodeX = 0;
        public float CustomerPostalCodeX { get { return customerPostalCodeX; } set { customerPostalCodeX = value; } }
        float customerPostalCodeY = 0;
        public float CustomerPostalCodeY { get { return customerPostalCodeY; } set { customerPostalCodeY = value; } }
        //BillTotalString
        string billTotalString = "";
        public String BillTotalString { get { return billTotalString; } set { billTotalString = value; } }
        float billTotalStringX = 0;
        public float BillTotalStringX { get { return billTotalStringX; } set { billTotalStringX = value; } }
        float billTotalStringY = 0;
        public float BillTotalStringY { get { return billTotalStringY; } set { billTotalStringY = value; } }
        //BillSubTotal
        string billSubTotal = "";
        public String BillSubTotal { get { return billSubTotal; } set { billSubTotal = value; } }
        float billSubTotalX = 0;
        public float BillSubTotalX { get { return billSubTotalX; } set { billSubTotalX = value; } }
        float billSubTotalY = 0;
        public float BillSubTotalY { get { return billSubTotalY; } set { billSubTotalY = value; } }
        //BillTax
        string billTax = "";
        public String BillTax { get { return billTax; } set { billTax = value; } }
        float billTaxX = 0;
        public float BillTaxX { get { return billTaxX; } set { billTaxX = value; } }
        float billTaxY = 0;
        public float BillTaxY { get { return billTaxY; } set { billTaxY = value; } }
        //billTotal
        string billTotal = "";
        public String BillTotal { get { return billTotal; } set { billTotal = value; } }
        float billTotalX = 0;
        public float BillTotalX { get { return billTotalX;  } set { billTotalX = value; } }
        float billTotalY = 0;
        public float BillTotalY { get { return billTotalY;  } set { billTotalY = value; } }
        #endregion

        #region HeaderBill
        public void SetBillID(string Value, float XPosition, float YPosition) {
            billID = Value;
            billIDX = XPosition;
            billIDY = YPosition;
        }
        public void SetBillDate(string Value, float XPosition, float YPosition) {
            billDate = Value;
            billDateX = XPosition;
            billDateY = YPosition;
        }
        public void SetBillSubTotal(string Value, float XPosition, float YPosition) {
            billSubTotal = Value;
            billSubTotalX = XPosition;
            billSubTotalY = YPosition;
        }
        public void SetBillTax(string Value, float XPosition, float YPosition) {
            billTax = Value;
            billTaxX = XPosition;
            billTaxY = YPosition;
        }
        public void SetBillTotal(string Value, float XPosition, float YPosition) {
            billTotal = Value;
            billTotalX = XPosition;
            billTotalY = YPosition;
        }
        public void SetBillTotalString(string Value, float XPosition, float YPosition) {
            billTotalString = Value;
            billTotalStringX = XPosition;
            billTotalStringY = YPosition;
        }
        public void SetCustomerName(string Value, float XPosition, float YPosition) {
            customerName = Value;
            customerNameY = YPosition;
            customerNameX = XPosition;
        }
        public void SetCustomerAddress(string Value, float XPosition, float YPosition) {
            customerAddress = Value;
            customerAddressX = XPosition;
            customerAddressY = YPosition;
        }
        public void SetCustomerCity(string Value, float XPosition, float YPosition) {
            customerCity = Value;
            customerCityX = XPosition;
            customerCityY = YPosition;
        }
        public void SetCustomerRFC(string Value, float XPosition, float YPosition) {
            customerRFC = Value;
            customerRFCX = XPosition;
            customerRFCY = YPosition;
        }
        public void SetCustomerPostalCode(string Value, float XPosition, float YPosition) {
            customerPostalCode = Value;
            customerPostalCodeX = XPosition;
            customerPostalCodeY = YPosition;
        }
        #endregion

       

        public void AddItem(string cantidad, string item, string price, string unitPrice)
        {
            BillOrderItem newItem = new BillOrderItem('?');
            items.Add(newItem.GenerateItem(cantidad, item, price,unitPrice));
        }

       
        private void DrawEspacio()
        {
            line = "";

            cb.SetTextMatrix(idProductX, YPosition());
            cb.SetFontAndSize(font.BaseFont, fontSize);
            cb.ShowText(line);

            startItemsOn++;
        }

        public bool Print()
        {
            try
            {
                //aqui para generar el PDF
                writer = PdfWriter.GetInstance(myBill, new FileStream(path + file_name, FileMode.Create));
                
                myBill.Open();
                cb = writer.DirectContent;
                cb.SetFontAndSize(font.BaseFont, fontSize);
                cb.BeginText();


                DrawInfo();
                DrawItems();
                

                cb.EndText();
                myBill.Close();

                return true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        private void DrawInfo() {
            //CustomerName
            cb.SetTextMatrix(customerNameX, myBill.PageSize.Height - customerNameY);
            cb.ShowText(customerName);
            //BillID
            cb.SetTextMatrix(billIDX, myBill.PageSize.Height - billIDY);
            cb.ShowText(billID);
            //BillDate
            cb.SetTextMatrix(billDateX, myBill.PageSize.Height - billDateY);
            cb.ShowText(billDate);
            //CustomerAddress
            cb.SetTextMatrix(customerAddressX, myBill.PageSize.Height - customerAddressY);
            cb.ShowText(customerAddress);
            //CustomerCity
            cb.SetTextMatrix(customerCityX, myBill.PageSize.Height - customerCityY);
            cb.ShowText(customerCity);
            //CustomerRFC
            cb.SetTextMatrix(customerRFCX, myBill.PageSize.Height - customerRFCY);
            cb.ShowText(customerRFC);
            //CustomerPostalCode
            cb.SetTextMatrix(customerPostalCodeX, myBill.PageSize.Height - customerPostalCodeY);
            cb.ShowText(customerPostalCode);
            //TotalString
            cb.SetTextMatrix(billTotalStringX, myBill.PageSize.Height - billTotalStringY);
            cb.ShowText(billTotalString);
            //SubTotal
            //cb.SetTextMatrix(billSubTotalX, myBill.PageSize.Height - billSubTotalY);
            //cb.ShowText(billSubTotal);
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, billSubTotal, billSubTotalX, myBill.PageSize.Height - billSubTotalY, 0);
            //Tax
            //cb.SetTextMatrix(billTaxX, myBill.PageSize.Height - billTaxY);
            //cb.ShowText(billTax);
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, billTax, billTaxX, myBill.PageSize.Height - billTaxY, 0);
            //Total
            //cb.SetTextMatrix(billTotalX, myBill.PageSize.Height - billTotalY);
            //cb.ShowText(billTotal);
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, billTotal, billTotalX, myBill.PageSize.Height - billTotalY, 0);

        }
        private float YPosition()
        {
            return (myBill.PageSize.Height - (topMargin + (startItemsOn * font.CalculatedSize + imageHeight)));
        }

        private void DrawItems()
        {
            
            BillOrderItem ordIt = new BillOrderItem('?');

            foreach (string item in items)
            {
                //cantidad
                line = ordIt.GetItemCantidad(item);
                cb.SetTextMatrix(qtyProductX, YPosition());
                cb.ShowText(line);

                //precio(Monto)
                line = ordIt.GetItemPrice(item);
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, line, amountProductX, YPosition(), 0);

                //PRECIO_VENTA
                line = ordIt.GetItemUnitPrice(item);
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, line, priceProductX, YPosition(), 0);

                //Descripcion
                string name = ordIt.GetItemName(item);
                
                if (name.Length > maxCharDescription)
                {
                    int currentChar = 0;
                    int itemLenght = name.Length;

                    while (itemLenght > maxCharDescription)
                    {
                        line = ordIt.GetItemName(item);

                        cb.SetTextMatrix(descProductX, YPosition());
                        cb.ShowText(line.Substring(currentChar, maxCharDescription));

                        startItemsOn++;
                        currentChar += maxCharDescription;
                        itemLenght -= maxCharDescription;
                    }

                    line = ordIt.GetItemName(item);

                    cb.SetTextMatrix(descProductX, YPosition());
                    cb.ShowText(line.Substring(currentChar, line.Length - currentChar));

                    startItemsOn++;
                }
                else
                {
                    cb.SetTextMatrix(descProductX, YPosition());
                    cb.ShowText(ordIt.GetItemName(item));

                    startItemsOn++;
                }
            }

        }
    
    }

    public class BillOrderItem
    {
        char[] delimitador = new char[] { '?' };

        public BillOrderItem(char delimit)
        {
            delimitador = new char[] { delimit };
        }
        //CANTIDAD
        public string GetItemCantidad(string BillOrderItem)
        {
            string[] delimitado = BillOrderItem.Split(delimitador);
            return delimitado[0];
        }
        //DESCRIPCION
        public string GetItemName(string BillOrderItem)
        {
            string[] delimitado = BillOrderItem.Split(delimitador);
            return delimitado[1];
        }
        //IMPORTE
        public string GetItemPrice(string BillOrderItem)
        {
            string[] delimitado = BillOrderItem.Split(delimitador);
            return delimitado[2];
        }
        //precio unitario
        public string GetItemUnitPrice(string BillOrderItem)
        {
            string[] delimitado = BillOrderItem.Split(delimitador);
            return delimitado[3];
        }
        //GENERAR
        public string GenerateItem(string cantidad, string itemName, string price, string unitPrice)
        {
            return cantidad + delimitador[0] + itemName + delimitador[0] + price + delimitador[0] + unitPrice;
        }
    }  
}

Please visit :www.TyroDeveloper.com

Please Click on +1

No comments:

Post a Comment