Tuesday 5 November 2013

Word Wrap in C# During Printing

This Code will word Wrap during printing




Code On This button

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace CA
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text.Length.ToString();

            PrintDocument doc = new PrintDocument();
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
            PrintDialog dlgSettings = new PrintDialog();
            dlgSettings.Document = doc;
            doc.Print();
        }
        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                //PictureBox abc = pictureBox1;

                Font font5 = new Font("Arial", 6);
                Font font4 = new Font("Arial", 7);
                Font font3 = new Font("Arial", 12);
                Font font2 = new Font("Arial", 8);
                Font font1 = new Font("Arial", 14);
                Font font = new Font("Arial", 10);
                Font font7 = new Font("Arial", 26);
                Font font8 = new Font("Arial", 30);
                Font font6 = new Font("Arial", 17);
                //float x = e.MarginBounds.Left;
                //float lineSide=font.
                float y = e.MarginBounds.Top;
                float lineHeight = font.GetHeight(e.Graphics);
                //e.Graphics.DrawImage(abc.Image, 70, 60, 130, 130);
                y += lineHeight;
                //e.Graphics.DrawImageUnscaled(adc, 0, 0, 768, 1152);
                e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------------------------------------" +
                    "-", font, Brushes.Black, float.Parse("40"), float.Parse("20"));
                y += lineHeight;
                int j = 28;
                while (j < 1070)
                {

                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("40"), float.Parse(j.ToString()));
                    y += lineHeight;
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("800"), float.Parse(j.ToString()));
                    y += lineHeight;
                    j = j + 12;

                }
                e.Graphics.DrawString("Ph.: 0124-4045552", font3, Brushes.Black, float.Parse("640"), float.Parse("35"));
                y += lineHeight;
                e.Graphics.DrawString("BILL", font1, Brushes.Black, float.Parse("390"), float.Parse("30"));
                y += lineHeight;
                e.Graphics.DrawString("VSHARP & Co.", font7, Brushes.Green, float.Parse("300"), float.Parse("55"));
                y += lineHeight;
                e.Graphics.DrawString("Chartered Accountants", font1, Brushes.Black, float.Parse("330"), float.Parse("95"));
                y += lineHeight;
                e.Graphics.DrawString("Branch Off:SCO-126, Aap Ka Bazar, Gurudwara Road ", font, Brushes.Black, float.Parse("255"), float.Parse("125"));
                y += lineHeight;
                e.Graphics.DrawString("Gurgaon -122001 (Hr.)", font, Brushes.Black, float.Parse("355"), float.Parse("145"));
                y += lineHeight;
                e.Graphics.DrawString("Ph.: 0124-4045552, 2225610 Telefax : 0124-4045552", font, Brushes.Black, float.Parse("260"), float.Parse("165"));
                y += lineHeight;
                e.Graphics.DrawString("S.T.Reg.No.- AAIFV1069JSD001", font3, Brushes.Black, float.Parse("50"), float.Parse("200"));
                y += lineHeight;
                e.Graphics.DrawString("PAN NO. AAIFV1069J", font3, Brushes.Black, float.Parse("610"), float.Parse("200"));
                y += lineHeight;
                e.Graphics.DrawString("------------------------------------------------------------------------------------------------------------------------------------------------------------------" +
                   "-", font, Brushes.Black, float.Parse("40"), float.Parse("214"));
                y += lineHeight;
                int k = 220;
                while (k < 300)
                {

                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("555"), float.Parse(k.ToString()));
                    y += lineHeight;
                    k = k + 12;
                }



                //e.Graphics.DrawString("Name     ", font3, Brushes.Black, float.Parse("50"), float.Parse("230"));
                //y += lineHeight;
                //e.Graphics.DrawString(" : " + textBox4.Text, font, Brushes.Black, float.Parse("120"), float.Parse("230"));
                //y += lineHeight;
                //e.Graphics.DrawString("Address  ", font3, Brushes.Black, float.Parse("50"), float.Parse("250"));
                //y += lineHeight;
                //e.Graphics.DrawString(" : " + textBox1.Text, font, Brushes.Black, float.Parse("120"), float.Parse("253"));
                //y += lineHeight;
                //e.Graphics.DrawString("Bill No.  ", font3, Brushes.Black, float.Parse("565"), float.Parse("235"));
                //y += lineHeight;
                //e.Graphics.DrawString(" : " + label25.Text, font3, Brushes.Black, float.Parse("615"), float.Parse("235"));
                //y += lineHeight;
                //e.Graphics.DrawString("Date  ", font3, Brushes.Black, float.Parse("565"), float.Parse("270"));
                //y += lineHeight;
                //e.Graphics.DrawString(" : " + dateTimePicker1.Value.ToShortDateString(), font3, Brushes.Black, float.Parse("615"), float.Parse("270"));
                //y += lineHeight;


                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("40"), float.Parse("299"));
                y += lineHeight;
                e.Graphics.DrawString("S.No.", font3, Brushes.Black, float.Parse("50"), float.Parse("312"));
                y += lineHeight;
                e.Graphics.DrawString("Particulars", font3, Brushes.Black, float.Parse("300"), float.Parse("312"));
                y += lineHeight;

                e.Graphics.DrawString("Amount", font3, Brushes.Black, float.Parse("660"), float.Parse("312"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("40"), float.Parse("324"));
                y += lineHeight;

                e.Graphics.DrawString("", font3, Brushes.Black, float.Parse("660"), float.Parse("360"));
                y += lineHeight;

                // The Red Color Text are the code of Word Wrap

                //==============================================================

                string ss = textBox1.Text;
                int count = textBox1.Text.Length;
                int divlength = 60;
                int remainder=count%divlength;

                int NoOfRow = 0;
                if (remainder > 0)
                {
                    NoOfRow = (count / divlength) + 1;
                }
                else
                {
                    NoOfRow = count / divlength;
                }
                string[] arr = new string[NoOfRow];
                for(int i=0;i<NoOfRow;i++)
                {
                    if (i < NoOfRow - 1)
                    {
                        arr[i] = ss.Substring(60 * i, 60);
                    }
                    else
                    {
                        arr[i] = ss.Substring(60 * i,remainder);
                    }
                }
                int cd = 350;
                for (int i = 0; i < NoOfRow; i++)
                {
                    e.Graphics.DrawString(arr[i], font3, Brushes.Brown, float.Parse("100"), float.Parse(cd.ToString()));
                    y += lineHeight;
                    cd = cd + 20;
                }

                //=============================================================

                int r = 332;
                while (r < 674)
                {
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("90"), float.Parse(r.ToString()));
                    y += lineHeight;

                    r = r + 12;
                }
                int s = 680;
                while (s < 798)
                {
                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("470"), float.Parse(s.ToString()));
                    y += lineHeight;

                    s = s + 12;
                }
                int a = 332;
                while (a < 794)
                {

                    e.Graphics.DrawString("|", font, Brushes.Black, float.Parse("620"), float.Parse(a.ToString()));
                    y += lineHeight;

                    a = a + 12;
                }

               // int c = 340;
                //for (int b = 0; b < dataGridView1.Rows.Count - 1; b++)
                //{
                //    e.Graphics.DrawString((b + 1).ToString(), font3, Brushes.Black, float.Parse("55"), float.Parse(c.ToString()));
                //    y += lineHeight;
                //    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[0].Value.ToString(), font3, Brushes.Black, float.Parse("100"), float.Parse(c.ToString()));
                //    y += lineHeight;
                //    e.Graphics.DrawString(dataGridView1.Rows[b].Cells[1].Value.ToString(), font3, Brushes.Black, float.Parse("660"), float.Parse(c.ToString()));
                //    y += lineHeight;


                //    c = c + 20;
                //}

                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("40"), float.Parse("1066"));
                y += lineHeight;

                //double pos = alin(textBox1.Text);
                //e.Graphics.DrawString(textBox1.Text, font, Brushes.Black, float.Parse(pos.ToString()), float.Parse("500"));

                //double pos = alin(textBox5.Text);
                //double pos1 = alin(textBox6.Text);
                //double pos2 = alin(textBox9.Text);
                //double pos3 = alin(textBox11.Text);
                //double pos4 = alin(textBox12.Text);
                e.Graphics.DrawString("Rs. in words", font3, Brushes.Black, float.Parse("50"), float.Parse("684"));
                y += lineHeight;
               // e.Graphics.DrawString(textBox20.Text, font3, Brushes.Black, float.Parse("70"), float.Parse("708"));
                y += lineHeight;
                e.Graphics.DrawString("Total", font, Brushes.Black, float.Parse("474"), float.Parse("684"));
                y += lineHeight;
               // e.Graphics.DrawString(textBox5.Text, font, Brushes.Black, float.Parse(pos.ToString()), float.Parse("684"));
                y += lineHeight;
               // e.Graphics.DrawString("Service Tax     " + textBox7.Text + "%", font, Brushes.Black, float.Parse("474"), float.Parse("708"));
                y += lineHeight;
                //e.Graphics.DrawString(textBox6.Text, font, Brushes.Black, float.Parse(pos1.ToString()), float.Parse("708"));
                y += lineHeight;
               // e.Graphics.DrawString("Ed Cess            " + textBox8.Text + "%", font, Brushes.Black, float.Parse("474"), float.Parse("732"));
                y += lineHeight;
               // e.Graphics.DrawString(textBox9.Text, font, Brushes.Black, float.Parse(pos2.ToString()), float.Parse("732"));
                y += lineHeight;
               // e.Graphics.DrawString("H.Ed Cess        " + textBox10.Text + "%", font, Brushes.Black, float.Parse("474"), float.Parse("756"));
                y += lineHeight;
               // e.Graphics.DrawString(textBox11.Text, font, Brushes.Black, float.Parse(pos3.ToString()), float.Parse("756"));
                y += lineHeight;
                e.Graphics.DrawString("Grand Total", font, Brushes.Black, float.Parse("474"), float.Parse("782"));
                y += lineHeight;

                //e.Graphics.DrawString(textBox12.Text, font, Brushes.Black, float.Parse(pos4.ToString()), float.Parse("782"));
                y += lineHeight;
                e.Graphics.DrawString("Pay By Cheque Only", font, Brushes.Black, float.Parse("50"), float.Parse("782"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("40"), float.Parse("812"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("40"), float.Parse("672"));
                y += lineHeight;
                e.Graphics.DrawString("-----------------------------------------------------------------------", font, Brushes.Black, float.Parse("470"), float.Parse("696"));
                y += lineHeight;
                e.Graphics.DrawString("-----------------------------------------------------------------------", font, Brushes.Black, float.Parse("470"), float.Parse("720"));
                y += lineHeight;
                e.Graphics.DrawString("-----------------------------------------------------------------------", font, Brushes.Black, float.Parse("470"), float.Parse("744"));
                y += lineHeight;
                e.Graphics.DrawString("-----------------------------------------------------------------------", font, Brushes.Black, float.Parse("470"), float.Parse("768"));
                y += lineHeight;
                e.Graphics.DrawString("Bank Details For TRF/RTGS", font, Brushes.Black, float.Parse("50"), float.Parse("802"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("40"), float.Parse("792"));
                y += lineHeight;
                //e.Graphics.DrawString("Account Name       " + comboBox3.Text, font, Brushes.Black, float.Parse("55"), float.Parse("830"));
                //y += lineHeight;
                //e.Graphics.DrawString("Account Number    " + textBox13.Text, font, Brushes.Black, float.Parse("55"), float.Parse("855"));
                //y += lineHeight;
                //e.Graphics.DrawString("Bank Name            " + comboBox1.Text, font, Brushes.Black, float.Parse("55"), float.Parse("880"));
                //y += lineHeight;
                //e.Graphics.DrawString("Bank Address        " + textBox15.Text, font, Brushes.Black, float.Parse("55"), float.Parse("905"));
                //y += lineHeight;
                //e.Graphics.DrawString("MICR Code            " + textBox16.Text, font, Brushes.Black, float.Parse("55"), float.Parse("930"));
                //y += lineHeight;
                //e.Graphics.DrawString("BSR Code             " + textBox17.Text, font, Brushes.Black, float.Parse("55"), float.Parse("955"));
                //y += lineHeight;
                //e.Graphics.DrawString("IFSC Code            " + textBox18.Text, font, Brushes.Black, float.Parse("55"), float.Parse("980"));
                y += lineHeight;
                e.Graphics.DrawString("-------------------------------------------------------------------------------------------------------------------------------------------------------------------", font, Brushes.Black, float.Parse("40"), float.Parse("1030"));
                y += lineHeight;
                //e.Graphics.DrawString("Branch Off. :- 2271 Jewali Bazar Rewari - 123401  Email:- casinghal@hotmail.com, 9896602646   ", font3, Brushes.Black, float.Parse("55"), float.Parse("1040"));
                //y += lineHeight;
                //e.Graphics.DrawString("                      Email:- casinghal@hotmail.com, 9896602646", font3, Brushes.Black, float.Parse("25"), float.Parse("1030"));
                //y += lineHeight;
                e.Graphics.DrawString("For VSHARP & Co", font, Brushes.Black, float.Parse("570"), float.Parse("830"));
                y += lineHeight;
                e.Graphics.DrawString("Partner/Authorised Signatory", font, Brushes.Black, float.Parse("595"), float.Parse("880"));
                y += lineHeight;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public double alin(string str)
        {
            Font font3 = new Font("Arial", 10);

            Size textsiz = TextRenderer.MeasureText(str, font3);
            double i = Convert.ToDouble(textsiz.Width);
            i = i + 3;
            double j = 800 - i;
            if (str.Length > 8)
            {
                j = j + 2;
                return j;
            }
            else if (str.Length > 5)
            {
                j = j + 1;
                return j;
            }
            else
            {
                return j;
            }
        }
    }
}

output



No comments:

Post a Comment