Solved

Going to New Page in Ms Word using C#

Posted on 2013-05-15
4
366 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now