Solved

Going to New Page in Ms Word using C#

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

17 Experts available now in Live!

Get 1:1 Help Now