Opt In (Do Not Edit Here)

Showing posts with label Windows Application. Show all posts
Showing posts with label Windows Application. Show all posts

Sep 14, 2012

Stream I/O Operation (Part-II)

The Blog demonstrate a method to Copy Multiple Text Documents to a Single Text Document, using StreamReader & StreamWriter Classes.

Description:

About Classes used -

#1: StreamReader class provides an access to read the data from Stream such as Text File.
#2: StreamWriter class provides an access to write the data from Stream such as Text File.


Namespace Required         -           System.IO,
System.Diagnostics

Controls Used           -          
            1. TextBox Control (txtOutput)
          2. ListBox Control (lbInput)
          2. Button Control (btnCopy, btnInputFile)
           
         
Here I implemented the Code for converting multiple Text Documents into single Text Document.

           
The Code:
1. Variable Declarations
        StreamReader rdr;   //StreamReader Object for Reading File
        StreamWriter wrt;   //StreamWriter Object for Writing File
Listing 1

2. Select the Input Text Files (code for “Select Input Files” Button).
           
            using (OpenFileDialog file = new OpenFileDialog())
            {
                file.Filter = "Only Text Documents|*.txt";

                //Allow to select multiple files
                file.Multiselect = true;

                //Show the Dialog Box & Select the Files
                file.ShowDialog();

                //Add the selected File Names to ListBox
                lbInput.Items.AddRange(file.FileNames);
            }
Listing 2
   

3. Copy Data from Multiple Text Files to Single Text File (code for “Copy All” Button).

            //Output File
            wrt = new StreamWriter(txtOutput.Text, false);

            //Copy All Data From Input file to Output file
            foreach (String var in lbInput.Items)
            {
                //Input File
                rdr = new StreamReader(var);

                //Start of New File
                wrt.WriteLine(Environment.NewLine +
                                    "File Name : " + var + Environment.NewLine);

                //Copy the Content of Input File(s) to Output File
                wrt.Write(rdr.ReadToEnd().ToString());
            }

            //Close the StreamWriter instance
            wrt.Close();

            MessageBox.Show("File Copied Successfully....");

            //Open the Output file
            Process.Start(txtOutput.Text);
Listing 3


4. Now execute the Application and see the result (Figure 1).
         
Intended Result: 

Figure 1

Summary:

          In this piece of writing, using C# environment, we have seen basic stream input/output operation for copying multiple text files to single text file using StreamWriter & StreamReader classes.

Sep 8, 2012

Convert Text Document to PDF File



 The Blog demonstrate a method to convert the text files into PDF files using iTextSharp PDF library.
Description:

About Classes used -

#1: StreamReader class provides an access to read the data from Stream such as Text File.
#2: Document class allows creating a new instance for Creating PDF File.
#3: PdfWriter class, an instantaneous access to write a PDF document from an object of Document class.


Namespace Required         -           System.IO,
iTextSharp,
iTextSharp.text,
iTextSharp.text.pdf


Controls Used           -          
            1. TextBox Control (txtInput, txtOutput)
          2. Button Control (btnSelect, btnCreatePDF)
           
         
Here I implemented the Code for converting Text Document into PDF Document using iTextSharp Tool.

           
The Code:
1. Select the Text File (code for “Select File” Button).
           
           
      using (OpenFileDialog file = new OpenFileDialog())
            {
                //Show the Dialog Box & Select the File
                file.ShowDialog();

                //Assign Input FileName to TextBox
                txtInput.Text = file.FileName;
            }

            //Assign Output FileName to TextBox
            txtOutput.Text = (txtInput.Text).Replace(".txt", ".pdf");
Listing 1
   

2. Convert Text File into a PDF File (code for “Create PDF” Button).

            //Read the Data from Input File
            StreamReader rdr = new StreamReader(txtInput.Text);

            //Create a New instance on Document Class
            Document doc = new Document();

            //Create a New instance of PDFWriter Class for Output File
            PdfWriter.GetInstance(doc, new FileStream(txtOutput.Text,FileMode.Create));

            //Open the Document
            doc.Open();

            //Add the content of Text File to PDF File
            doc.Add(new Paragraph(rdr.ReadToEnd()));

            //Close the Document
            doc.Close();

            MessageBox.Show("Conversion Successful....");

            //Open the Converted PDF File
            System.Diagnostics.Process.Start(txtOutput.Text);
Listing 2


3. Now execute the Application and see the result (Figure 1).
         
Intended Result:

Figure 1


Summary:

          In this piece of writing, using C# environment, we have seen how to convert the Text file into PDF file using iTextSharp Tool (A free PDF library ported from Java iText).

Stream I/O Operation (Part-I)



 Blog demonstrate a way to Copy / Append the one Text Document to another, using StreamReader & StreamWriter Classes. 


Description:

About Classes used -

#1: StreamReader class provides an access to read the data from Stream such as Text File.
#2: StreamWriter class provides an access to write the data from Stream such as Text File.


Namespace Required         -           System.IO,
System.Diagnostics

Controls Used           -          
            1. TextBox Control (txtInput, txtOutput)
          2. Button Control (btnSelect, btnCreatePDF)
           
         
Here I implemented the Code for copying and appending Text Document to another Text Document.

           
The Code:
1. Variable Declarations
        StreamReader rdr;   //StreamReader Object for Reading File
        StreamWriter wrt;   //StreamWriter Object for Writing File
Listing 1

1. Select the Text Input & Output File (code both for “Select” Button).
           
            //Select File with OpenFileDialog Box
            String SelectFile(TextBox t)
            {
                  using (OpenFileDialog file = new OpenFileDialog())
                  {
                        file.Filter = "Only Text Documents|*.txt";

                        //Show the Dialog Box & Select the File
                        file.ShowDialog();

                        //Return the selected File Name as String
                        return file.FileName;
                  }
      }
Listing 2
   

3. Copy Data from one to another Text File (code for “Copy All” Button).

            //Input File
            rdr = new StreamReader(txtInput.Text);

            //Output File
            wrt = new StreamWriter(txtOutput.Text, false);

            //Copy All Data From Input file to Output file
            wrt.Write(rdr.ReadToEnd().ToString());

            //Close the StreamWriter instance
            wrt.Close();

            MessageBox.Show("File Copied Successfully....");

            //Open the Output file
            Process.Start(txtOutput.Text);
Listing 2


4. Append Data from one to another Text File (code for “Append All” Button).
            //Input File
            rdr = new StreamReader(txtInput.Text);

            //Output File
            wrt = new StreamWriter(txtOutput.Text, true);

            //Append All Data From Input file to Output file
            wrt.WriteLine(Environment.NewLine +
                "--------------- Appended File Start from here ---------------" + 
      Environment.NewLine);
            wrt.Write(rdr.ReadToEnd().ToString());

            //Close the StreamWriter instance
            wrt.Close();

            MessageBox.Show("File Appended Successfully....");

            //Open the Output file
            Process.Start(txtOutput.Text);
Listing 3

3. Now execute the Application and see the result (Figure 1).
         
Intended Result:

Figure 1

Summary:

          In this piece of writing, using C# environment, we have seen basic stream input/output operation for copying & appending text files using StreamWriter & StreamReader classes.

All Rights Reserved. 2014 Copyright SIMPLITONA

Powered By Blogger | Published By Gooyaabi Templates Designed By : BloggerMotion

Top