Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Going to New Page in Ms Word using C#

Posted on 2013-05-15
4
381 Views
Last Modified: 2013-05-16
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

0
Comment
Question by:Dinesh Kumar
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 39170289
Not sure if word will honor it-- and don't have either word or mono installed on this computer to test it out-- but you could try inserting an ascii formfeed (chr(12) or "\f") into your text file.  
...
                        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("\f");
                                fileWriter.WriteLine(dr[i].ToString());
                                fileWriter.WriteLine(System.Environment.NewLine);
                            }
                        }

Open in new window

0
 

Author Comment

by:Dinesh Kumar
ID: 39170674
I want to go on new page instead of new line.
0
 

Author Comment

by:Dinesh Kumar
ID: 39170740
fileWriter.WriteLine("\f");  

Worked!

Thank You.
0
 

Author Closing Comment

by:Dinesh Kumar
ID: 39170743
To the Point Solution!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question