Solved

Going to New Page in Ms Word using C#

Posted on 2013-05-15
4
393 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
[X]
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
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

628 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