Dec 13, 2011

Printing Indian Cheque Application in Silverlight

Hello Friends,

This time something different but a useful Printing application, I have developed using silverlight for our office use.

Our office admin, required cheque details printing utility, there were lots available in market, to download free of cost, but as usual for customization it charges. Also, there are desktop utility as well, I felt to make this facility live over web. Hence started this application

Code inside MainPage.xaml:

<UserControl x:Class="SL_Printing_Cheque.MainPage"
    d:DesignHeight="500" d:DesignWidth="800" xmlns:toolkit="" xmlns:sdk="">

    <Grid x:Name="LayoutRoot" Background="White">
            <Converters:Converter_Visibility x:Key="Converter_Visibility"/>
            <RowDefinition Height="50" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="260*" />
        <TextBlock Text="Cheque Printing Application" FontSize="16" FontWeight="Bold" Margin="10"/>

        <toolkit:Expander Grid.Row="1" Name="expander1"
                              ExpandDirection="Down" IsExpanded="False"
                              Header=" Configurations " >
            <Grid  HorizontalAlignment="Stretch" Name="grid1" VerticalAlignment="Stretch" Width="Auto" >
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                    <ColumnDefinition Width="70"/>
                    <ColumnDefinition Width="70"/>
                    <ColumnDefinition Width="70"/>

                <TextBlock Grid.Row="0" Grid.Column="0" Text="Template:"/>
                <TextBlock Grid.Row="0" Grid.Column="4" Text="Company:"/>
                <TextBlock Grid.Row="1" Grid.Column="0" Text="Party:"/>
                <TextBlock Grid.Row="1" Grid.Column="4" Text="Auth Person:"/>
                <TextBlock Grid.Row="2" Grid.Column="0" Text="Amount:"/>
                <TextBlock Grid.Row="2" Grid.Column="2" Text="Date:"/>
                <TextBlock Grid.Row="2" Grid.Column="4" Text="A/c No:"/>

                <TextBlock Grid.Row="0" Grid.Column="1" Text="Axis Bank Template"/>

                <TextBox Grid.Row="0" Grid.Column="5" Grid.ColumnSpan="2"   Text="XYZ India Pvt. Ltd." Name="txt_Company" TextWrapping="Wrap"/>

                <TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" x:Name="txt_Party" Text="Viral Rathod"/>

                <TextBox Grid.Row="1" Grid.Column="5" Grid.ColumnSpan="2" x:Name="txt_Auth_Person" Text="Mr. ABC "/>

                <TextBox Grid.Row="2" Grid.Column="1" Text="925" Name="txt_Amount"  LostFocus="txt_Amount_LostFocus"/>

                <sdk:DatePicker Grid.Row="2" Grid.Column="3" Name="dp_Date"  SelectedDateFormat="Short" SelectedDateChanged="dp_Date_SelectedDateChanged"/>

                <TextBox Grid.Row="2" Grid.Column="5" Name="txt_Ac_No"  Text="20955065633"/>

                <StackPanel Grid.Row="3" Grid.ColumnSpan="10" Orientation="Horizontal" >
                    <CheckBox x:Name="chkbx_AC_Pay" Content="A/c Pay  " IsChecked="True"/>
                    <CheckBox x:Name="chkbx_Bearer" Content="Bearer  " IsChecked="True"/>
                    <CheckBox x:Name="chkbx_Non_Negotiable" Content="Non Negotiable  " IsChecked="True"/>
                    <CheckBox x:Name="chkbx_Company" Content="Company &amp; Auth Person  " IsChecked="True"/>
                    <CheckBox x:Name="chkbx_AC_No" Content="A/c No.  " />

                    <Button Name="btnPrint"  Click="btnPrint_Click" Content="  Print  "/>

                    <Slider Name="slider_Opacity" Maximum="1" Minimum="0"  Width="100" Value="0.1" Margin="10,0,10,0"/>




        <Border Grid.Row="2" BorderBrush="Gray" BorderThickness="2" Background="Silver">
            <Canvas Name="canvas_PrintContent"  Background="White" Margin="20">
                <Image Source="/SL_Printing_Cheque;component/Images/axis.jpg" Opacity="{Binding ElementName=slider_Opacity,Path=Value}" Name="img"/>

                <TextBlock Visibility="{Binding ElementName=chkbx_AC_Pay, Path=IsChecked, Converter={StaticResource Converter_Visibility},  Mode=TwoWay}" Canvas.Left="83" Canvas.Top="23" Height="23" Text="A/c Pay Only" FontSize="14" FontWeight="Bold" FontFamily="Trebuchet MS" MouseLeftButtonDown="drag_MouseLeftButtonDown" />
                <TextBlock Visibility="{Binding ElementName=chkbx_Non_Negotiable, Converter={StaticResource Converter_Visibility}, Path=IsChecked, Mode=TwoWay}" Canvas.Left="176" Canvas.Top="23" Height="23" Text="Non Negotiable" FontSize="14" FontWeight="Bold" FontFamily="Trebuchet MS" MouseLeftButtonDown="drag_MouseLeftButtonDown" />

                <Border Name="tb_ACPAY"  Visibility="{Binding ElementName=chkbx_AC_Pay, Converter={StaticResource Converter_Visibility}, Path=IsChecked, Mode=TwoWay}"  Canvas.Left="81" Canvas.Top="23" Height="18" Width="88" BorderThickness="0,1,0,1" BorderBrush="Black"/>
                <Border Name="tb_NNEGO"  Visibility="{Binding ElementName=chkbx_Non_Negotiable, Converter={StaticResource Converter_Visibility}, Path=IsChecked, Mode=TwoWay}" Canvas.Left="173" Canvas.Top="23" Height="18"  Width="106" BorderThickness="0,1,0,1" BorderBrush="Black"/>

                <TextBlock Text=" 3  1 2  2 0 1 1" Canvas.Left="606" Canvas.Top="27" Name="tb_Date" FontFamily="Trebuchet MS" FontSize="11.90" MouseLeftButtonDown="drag_MouseLeftButtonDown" >
                        <ScaleTransform ScaleX="1.3"/>

                <TextBlock Text="{Binding ElementName=txt_Party, Path=Text}" Canvas.Left="66" Canvas.Top="66" FontFamily="Trebuchet MS" FontSize="14" MouseLeftButtonDown="drag_MouseLeftButtonDown" />

                <TextBlock x:Name="tb_Rupees" Text="Rs. Only" Canvas.Left="67" Canvas.Top="102" FontFamily="Trebuchet MS" FontSize="14" MouseLeftButtonDown="drag_MouseLeftButtonDown" />

                <TextBlock x:Name="tb_Ac_No" Text="{Binding ElementName=txt_Ac_No,Path=Text}" Canvas.Left="117" Canvas.Top="164" FontFamily="Trebuchet MS" FontSize="14" MouseLeftButtonDown="drag_MouseLeftButtonDown" />

                <StackPanel Canvas.Left="549" Canvas.Top="135" Orientation="Horizontal" MouseLeftButtonDown="drag_MouseLeftButtonDown">
                    <TextBlock Text=" **"  FontFamily="Trebuchet MS" FontSize="14"/>
                    <TextBlock Text="{Binding ElementName=txt_Amount, Path=Text}"  FontFamily="Trebuchet MS" FontSize="14"/>
                    <TextBlock Text="/--"  FontFamily="Trebuchet MS" FontSize="14"/>

                <TextBlock Text="{Binding ElementName=txt_Company, Path=Text}"
                           Visibility="{Binding ElementName=chkbx_Company, Converter={StaticResource Converter_Visibility}, Path=IsChecked, Mode=TwoWay}"
                           Canvas.Left="561" Canvas.Top="173" FontFamily="Trebuchet MS" FontSize="13"   MouseLeftButtonDown="drag_MouseLeftButtonDown" />

                <TextBlock Text="{Binding ElementName=txt_Auth_Person, Path=Text}"
                           Visibility="{Binding ElementName=chkbx_Company, Converter={StaticResource Converter_Visibility}, Path=IsChecked, Mode=TwoWay}"
                           Canvas.Left="561" Canvas.Top="247" FontFamily="Trebuchet MS" FontSize="13"   MouseLeftButtonDown="drag_MouseLeftButtonDown" />

                <TextBlock Visibility="{Binding ElementName=chkbx_Bearer, Converter={StaticResource Converter_Visibility}, Path=IsChecked, Mode=TwoWay}"  Text="XXXXXX" Canvas.Left="601" Canvas.Top="85" FontFamily="Trebuchet MS" FontSize="14" MouseLeftButtonDown="drag_MouseLeftButtonDown" />


Code Inside its CSharp page:
    public partial class MainPage : UserControl
        public MainPage()

        private void btnPrint_Click(object sender, RoutedEventArgs e)
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += doc_PrintPage;
            doc.Print("Cheque Print");
        void doc_PrintPage(object sender, PrintPageEventArgs e)
            img.Opacity = 0.0;
            e.PageVisual = canvas_PrintContent;

        private void txt_Amount_LostFocus(object sender, RoutedEventArgs e)
            int amount = Convert.ToInt32((sender as TextBox).Text);
            tb_Rupees.Text = "** " + Helper.ConvertAmountToSpelling(amount) + " Only **";

One Helper Class used to convert the Amount into String/text: 

    public static class Helper
        static  string[] units = new string[] { "ONE ", "TWO ", "THREE ", "FOUR ", "FIVE ", "SIX ", "SEVEN ", "EIGHT ", "NINE ", "TEN ", "ELEVEN ", "TWELEVE ", "THIRTEEN ", "FOURTEEN ", "FIFTEEN ", "SIXTEEN ", "SEVENTEEN ", "EIGHTEEN ", "NINTEEN " };
        static  string[] tens = new string[] { "TEN ", "TWENTY ", "THIRTY ", "FOURTY ", "FIFTY ", "SIXTY ", "SEVENTY ", "EIGHTY ", "NINTY " };
        static string[] thousands = new string[] { "TEN ", "TWENTY ", "THIRTY ", "FOURTY ", "FIFTY ", "SIXTY ", "SEVENTY ", "EIGHTY ", "NINTY " };
        static int i, num;

        public static string ConvertAmountToSpelling(int amount)
            string result = "";
            int len = amount.ToString().Length;
            num = amount;           

            if (num > 99 && num < 1000)
                i = num / 100;
                result = units[i - 1] + "HUNDRED ";
                num = num % 100;

            if (num > 19 && num < 100)
                i = num / 10;
                result = result + tens[i - 1];
                num = num % 10;

            if (num < 20 && num > 0)
                result += units[num - 1];

            return result;

 And Finally one Converter:

    public class Converter_Visibility : IValueConverter
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            if (System.Convert.ToBoolean(value))
                return Visibility.Visible;
                return Visibility.Collapsed;

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            throw new NotImplementedException();

That's it we are done with the Cheque Printing Application using Silverlight, Soon I will be trying to make this tiny but useful application over live.

No comments:

Post a Comment