Link to home
Create AccountLog in
Avatar of Dinesh Kumar
Dinesh KumarFlag for India

asked on

Going to New Page in Ms Word using C#

Hi,

I am working on creating Word File from Text File having some matter:

I want to go to new page on certain condition in Ms Word. How can I do it?

I am using the following class to create Dataset from a text File.

public class TextToDataset
    {
        public static DataSet Convert(string File,string TableName, string delimiter)
        {
     
            DataSet result = new DataSet();       
            StreamReader s = new StreamReader(File);        
            result.Tables.Add(TableName);         
            string AllData = s.ReadToEnd();        
            string[] rows = AllData.Split("\r\n".ToCharArray());         
            result.Tables[TableName].Columns.Add("DATA");
            foreach (string r in rows)
            {           
                string[] items = r.Split(delimiter.ToCharArray());              
                result.Tables[TableName].Rows.Add(items);
            }                
            return result;
        }
    }

Open in new window


and then calling as follows:

Please see the else part of the below code where i am trying to go to the new Page in the same file.

DataSet ds = TextToDataset.Convert(file, "MyNewTable", "\t");

    #region For Each Loop through data set
            //For loop through data set
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    if (dr[i].ToString() != "")
                    {
                        string[] bits = dr[0].ToString().Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        //if clean getting the value N
                        string IsClean = bits[1].ToString();
                        //checking whether the record is clean or not
                        if (IsClean == "N")
                        {
                            //writting clean record to the file
                            using (var fileWriter = File.AppendText(filefinal))
                            {
                                string[] clean = dr[i].ToString().Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                string licence = clean[0].ToString();
                                string character = clean[1].ToString();
                                string finallicence = clean[2].ToString();

                                fileWriter.WriteLine(clean[0].ToString() + " "+ clean[1].ToString() +" "+"Clean Record");
                                fileWriter.WriteLine(clean[2].ToString());
                                fileWriter.WriteLine(System.Environment.NewLine);
                            }
                        }
                        else
                        {
        [b]                    //want to write here on the  New Page but it does not go the new page Please Help!)[/b]
                            using (var fileWriter = File.AppendText(filefinal))
                            {
                                fileWriter.WriteLine(dr[i].ToString());
                                fileWriter.WriteLine(System.Environment.NewLine);
                            }
                        }
                    }

                }
            }
        }
        #endregion

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Dinesh Kumar

ASKER

I want to go on new page instead of new line.
fileWriter.WriteLine("\f");  

Worked!

Thank You.
To the Point Solution!