Wednesday, 26 December 2012

print in c#.net

 List<ShippingReport> ShippingDetails = new List<ShippingReport>();

        class ShippingReport
        {
            public string FromName { get; set; }
            public string FranchiserName { get; set; }
            public string FromAddress { get; set; }
            public string ToAddress { get; set; }
            public Image ZipBarCode { get; set; }
            public Image CourierBarcode { get; set; }
            public Image FranchiserBarCode { get; set; }
            public string StoreNo { get; set; }
            public Image StoreBarCode { get; set; }
            public Image ShippingBarCode { get; set; }
            public string CourierName { get; set; }
            public string CourierPhone { get; set; }
            public string CourierCode { get; set; }
            public string Quantity { get; set; }
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            //Reports.crReportShipping cobj = new Reports.crReportShipping();
            //cobj.SetDataSource(objdml.GetShippingHeader().ToList());
            //Reports.frmReportViewerNew frm = new Reports.frmReportViewerNew();
            //frm.crystalReportViewer1.ReportSource = cobj;
            //frm.ShowDialog();
            lblShippingCode.Text = "A0SRINeck12A0000026";
            if (lblShippingCode.Text != string.Empty)
            {
                GetShippingDetails();
                PrintReport();
            }
        }
        public void GetShippingDetails()
        {
            ShippingReport Shipping = new ShippingReport();
            Shipping.FromName = "Derewala Jewellery International";
            Shipping.FranchiserName = Cobfranchiser.Text;
            Shipping.Quantity = TotalItems();
            Shipping.ShippingBarCode = GenerateShippingBarcode(lblShippingCode.Text);
            Shipping.CourierName = CobCorier.Text;
            Shipping.CourierPhone = lblConPh.Text;
            Shipping.FromAddress = "Sitapura Industrial Area,\nJaipur,Rajsthan";
            string ToAddress = "";
            if (checkBox1.Checked == true)
            {
                Shipping.ZipBarCode = GenerateZipBarcode(LbsZip.Text);
                ToAddress += Lbsaddress1.Text;
                if (Lbsaddress2.Text != "")
                {
                    ToAddress += ",\n" + Lbsaddress2.Text;
                }
                ToAddress += ",\nCity : " + Lbscity.Text + ", State : " + Lbsstate.Text + ",\nZip code : " + LbsZip.Text + ",Country : " + Lbscountry.Text;
                Shipping.ToAddress = ToAddress;
            }
            else
            {
                Shipping.ZipBarCode = GenerateZipBarcode(TxtZip.Text);
                ToAddress += Txtaddress1.Text;
                if (Txtaddress2.Text != "")
                {
                    ToAddress += ",\n" + Txtaddress2.Text;
                }
                ToAddress += ",\nCity : " + CobCity.Text + ", State : " + CobState.Text + ",\nZip code : " + TxtZip.Text + ",Country : " + CobCountry.Text;
                Shipping.ToAddress = ToAddress;
            }
            Shipping.CourierBarcode = GenerateCourierBarcode("9937");
            Shipping.FranchiserBarCode = GenerateFranchiserBarcode("4536");
            Shipping.StoreNo = "2346";
            Shipping.StoreBarCode = GenerateStoreBarcode(Shipping.StoreNo);
            //Shipping.CourierCode=
            ShippingDetails.Add(Shipping);
        }

        private string TotalItems()
        {
            int TotalItems = 0;
            for (int i = 0; i < dglogisitic.Rows.Count; i++)
            {
                TotalItems += int.Parse(dglogisitic.Rows[i].Cells[4].Value.ToString());
            }
            return TotalItems.ToString();
        }

        SwashLibrary.Barcode b = new SwashLibrary.Barcode();

        private Image GenerateZipBarcode(string ZipCode)
        {
            Image GeneratedImg = null;
            try
            {
                int W = 140;
                int H = 60;
                b.Alignment = SwashLibrary.AlignmentPositions.CENTER;
                SwashLibrary.TYPE type = SwashLibrary.TYPE.UNSPECIFIED;
                type = SwashLibrary.TYPE.CODE128B;
                b.IncludeLabel = true;
                b.LabelPosition = SwashLibrary.LabelPositions.TOPCENTER;
                GeneratedImg = b.Encode(type, ZipCode, System.Drawing.Color.Black, System.Drawing.Color.White, W, H);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return GeneratedImg;
        }

        private Image GenerateCourierBarcode(string CourierCode)
        {
            Image GeneratedImg = null;
            try
            {
                int W = 140;
                int H = 60;
                b.Alignment = SwashLibrary.AlignmentPositions.CENTER;
                SwashLibrary.TYPE type = SwashLibrary.TYPE.UNSPECIFIED;
                type = SwashLibrary.TYPE.CODE128B;
                b.IncludeLabel = true;
                b.LabelPosition = SwashLibrary.LabelPositions.TOPCENTER;
                GeneratedImg = b.Encode(type, CourierCode, System.Drawing.Color.Black, System.Drawing.Color.White, W, H);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return GeneratedImg;
        }

        private Image GenerateFranchiserBarcode(string FranchiserCode)
        {
            Image GeneratedImg = null;
            try
            {
                int W = 140;
                int H = 60;
                b.Alignment = SwashLibrary.AlignmentPositions.CENTER;
                SwashLibrary.TYPE type = SwashLibrary.TYPE.UNSPECIFIED;
                type = SwashLibrary.TYPE.CODE128B;
                b.IncludeLabel = true;
                b.LabelPosition = SwashLibrary.LabelPositions.TOPCENTER;
                GeneratedImg = b.Encode(type, FranchiserCode, System.Drawing.Color.Black, System.Drawing.Color.White, W, H);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return GeneratedImg;
        }

        private Image GenerateStoreBarcode(string StoreCode)
        {
            Image GeneratedImg = null;
            try
            {
                int W = 140;
                int H = 60;
                b.Alignment = SwashLibrary.AlignmentPositions.CENTER;
                SwashLibrary.TYPE type = SwashLibrary.TYPE.UNSPECIFIED;
                type = SwashLibrary.TYPE.CODE128B;
                b.IncludeLabel = true;
                b.LabelPosition = SwashLibrary.LabelPositions.TOPCENTER;
                GeneratedImg = b.Encode(type, StoreCode, System.Drawing.Color.Black, System.Drawing.Color.White, W, H);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return GeneratedImg;
        }

        private Image GenerateShippingBarcode(string ShippingCode)
        {
            Image GeneratedImg = null;
            try
            {
                int W = 500;
                int H = 100;
                b.Alignment = SwashLibrary.AlignmentPositions.CENTER;
                SwashLibrary.TYPE type = SwashLibrary.TYPE.UNSPECIFIED;
                type = SwashLibrary.TYPE.CODE128B;
                b.IncludeLabel = true;
                b.LabelPosition = SwashLibrary.LabelPositions.TOPCENTER;
                GeneratedImg = b.Encode(type, ShippingCode, System.Drawing.Color.Black, System.Drawing.Color.White, W, H);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return GeneratedImg;
        }

        private void PrintReport()
        {
            if (System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count <= 0)
            {
                MessageBox.Show("Printer not found");
                return;
            }
            else
            {
                PrintDocument doc = new TextDocument();
                System.Drawing.Printing.PaperSize paperSize = new System.Drawing.Printing.PaperSize();
                paperSize.RawKind = (int)PaperKind.Custom;
                paperSize.Height = 720;
                paperSize.Width = 660;
                doc.DefaultPageSettings.PaperSize = paperSize;
                doc.DefaultPageSettings.Landscape = false;
                doc.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
                doc.PrintPage += this.Doc_PrintPage;
                doc.Print();
            }
        }

        class TextDocument : PrintDocument
        {
            public int PageNumber;
            public int Offset;
            public TextDocument()
            {
            }
        }

        private void Doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            TextDocument doc = (TextDocument)sender;
            Font font = new Font("Arial", 10);
            float lineHeight = 70;
            float x = e.MarginBounds.Left;
            float y = e.MarginBounds.Top;
            doc.PageNumber += 1;
            while ((y + lineHeight) < e.MarginBounds.Bottom && doc.Offset < ShippingDetails.Count)
            {
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y, x + 600, y);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y, x, y + 140);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 600, y, x + 600, y + 140);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 300, y, x + 300, y + 140);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 140, x + 600, y + 140);
                e.Graphics.DrawString("From", new Font("Calibri", 9, FontStyle.Regular), Brushes.Black, x + 10, y + 10);
                e.Graphics.DrawString(ShippingDetails[0].FromName, new Font("Calibri", 9, FontStyle.Bold), Brushes.Black, x + 20, y + 30);
                e.Graphics.DrawString(ShippingDetails[0].FromAddress, new Font("Calibri", 9, FontStyle.Regular), Brushes.Black, x + 20, y + 50);
                e.Graphics.DrawString("To", new Font("Calibri", 9, FontStyle.Regular), Brushes.Black, x + 310, y + 10);
                e.Graphics.DrawString(ShippingDetails[0].FranchiserName, new Font("Calibri", 9, FontStyle.Bold), Brushes.Black, x + 320, y + 30);
                e.Graphics.DrawString(ShippingDetails[0].ToAddress, new Font("Calibri", 9, FontStyle.Regular), Brushes.Black, x + 320, y + 50);
                //e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 140, x + 600, y + 140);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 140, x, y + 280);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 600, y, x + 600, y + 280);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 300, y, x + 300, y + 280);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 280, x + 600, y + 280);
                e.Graphics.DrawString("SHIP TO POSTAL CODE", new Font("Calibri", 11, FontStyle.Bold), Brushes.Black, x + 5, y + 145);
                Image img;
                img = ShippingDetails[0].ZipBarCode;
                e.Graphics.DrawImage(ShippingDetails[0].ZipBarCode, (300 - (img.Width)) / 2, (280 - img.Height));
                e.Graphics.DrawString("CARRIER", new Font("Calibri", 11, FontStyle.Bold), Brushes.Black, x + 305, y + 145);
                e.Graphics.DrawString(ShippingDetails[0].CourierName, new Font("Calibri", 9, FontStyle.Bold), Brushes.Black, x + 325, y + 165);
                e.Graphics.DrawString("Phone No", new Font("Calibri", 9, FontStyle.Bold), Brushes.Black, x + 325, y + 185);
                e.Graphics.DrawString(":", new Font("Calibri", 9, FontStyle.Bold), Brushes.Black, x + 400, y + 185);
                e.Graphics.DrawString(ShippingDetails[0].CourierPhone, new Font("Calibri", 9, FontStyle.Regular), Brushes.Black, x + 410, y + 185);
                img = ShippingDetails[0].FranchiserBarCode;
                e.Graphics.DrawImage(ShippingDetails[0].CourierBarcode, ((300 - (img.Width)) / 2) + 300, (280 - img.Height));
                //e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 280, x + 600, y + 280);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 280, x, y + 380);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 600, y, x + 600, y + 380);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 380, x + 600, y + 380);
                img = ShippingDetails[0].FranchiserBarCode;
                e.Graphics.DrawImage(img, ((300 - (img.Width)) / 2) + 300, y + 282);
                e.Graphics.DrawString("CASE QTY", new Font("Calibri", 9, FontStyle.Bold), Brushes.Black, x + 325, y + img.Height + 300);
                e.Graphics.DrawString(":", new Font("Calibri", 9, FontStyle.Bold), Brushes.Black, x + 390, y + img.Height + 300);
                e.Graphics.DrawString(ShippingDetails[0].Quantity, new Font("Calibri", 9, FontStyle.Regular), Brushes.Black, x + 404, y + img.Height + 300);
                //e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 380, x + 600, y + 380);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 380, x, y + 520);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 600, y, x + 600, y + 520);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 300, y + 380, x + 300, y + 520);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 520, x + 600, y + 520);
                e.Graphics.DrawString("STORE", new Font("Calibri", 11, FontStyle.Bold), Brushes.Black, x + 10, y + 385);
                img = ShippingDetails[0].StoreBarCode;
                e.Graphics.DrawImage(img, ((300 - (img.Width)) / 2), 400 + ((140 - img.Height) / 2));
                e.Graphics.DrawString("STORE #", new Font("Calibri", 24, FontStyle.Bold), Brushes.Black, x + 315, y + 410);
                e.Graphics.DrawString(ShippingDetails[0].StoreNo, new Font("Calibri", 24, FontStyle.Bold), Brushes.Black, x + 350, y + 480);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 520, x, y + 660);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x + 600, y, x + 600, y + 660);
                e.Graphics.DrawLine(new Pen(Color.Black, 1), x, y + 660, x + 600, y + 660);
                img = ShippingDetails[0].ShippingBarCode;
                e.Graphics.DrawImage(img, ((600 - (img.Width)) / 2), 520 + ((140 - img.Height) / 2));

                //e.Graphics.DrawImage(imgList[doc.Offset], x, y);
                //int off = doc.Offset;
                //doc.Offset += 1;
                //if (doc.Offset < imgList.Count)
                //{
                //    e.Graphics.DrawImage(imgList[doc.Offset], imgList[off].Width + 58, y);
                //}
                doc.Offset += 1;
                y += lineHeight;
            }

            if (doc.Offset < ShippingDetails.Count)
            {
                e.HasMorePages = true;
            }
            else
            {
                doc.Offset = 0;
            }
        }

No comments:

Post a Comment