none
Inserting a line break RRS feed

  • Question

  • Hello ladies and gents,

    Many of you may have used NotePad to save text data and made use of the Insert Line Breaks selector when saving the file.

    I'm trying to insert line breaks into a Microsoft Word doc in the same manner that NotePad inserts line breaks. I have tried to use RegEx to match values between /n /r /n/r and nothing is working. I did make use of Microsoft.Office.InterOp.Word Sentences but that property matches to the end of a period. I am trying to programmically insert a line break at the margin.

    I know this can be done because Notepad does it. Anyone know how with C#?

    • Moved by CoolDadTx Wednesday, May 9, 2018 2:09 PM Office related
    Wednesday, May 9, 2018 5:01 AM

Answers

  • Thanks for your reply Terry.  The suggested code Read from word document line by line uses Microsoft.Office.Interop.Word to read a .docx file line by line; the suggested code actually reads paragraph by paragraph. After some initial research I've learned that .docx file use the Open Xml format, and that the best reference to use is DocumentFormat.OpenXml

    Kind of learning as I go and this is where I am presently at. I shall provide usable updates as they come but if anyone has OpenXml pointers that can assist me with reading the text line by line that would be a big help.

    I am struggling with an output where each run being returned is kind of choppy, as if there are formatting tags preventing the line from being return as one continuous string. For anyone edification this code represents the tip of my spear.

    string cindy1 = @"K:\NicelyDone\Books\Cindy\Cindy1.docx";
    
    Package wordPackage = Package.Open(cindy1, FileMode.Open, FileAccess.Read);
    
                using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(wordPackage))
                {
                    Body body = wordDocument.MainDocumentPart.Document.Body;
                    using (FileStream fs = new FileStream(@"K:\Run.txt", FileMode.Create))
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        var parag = body.Elements<Paragraph>();
                        foreach (var para in parag)
                        {
                            foreach (var run in para.Elements<Run>())
                            {                            
                                foreach (var text in run.Elements<Text>())
                                {
                                    sw.WriteLine(run.InnerText);
                                }
                            }
                        }
                    }                    
                }
                wordPackage.Close();


    Robert Meyer

    • Marked as answer by BobbyBob711 Saturday, May 12, 2018 12:07 PM
    Thursday, May 10, 2018 6:27 PM

All replies

  • Ok, after sleeping on it I woke this morning and realized a better way to describe my objective.

    I am trying to use C# to read a word file.docx line by line. Has anyone ever done that?


    Robert Meyer

    Wednesday, May 9, 2018 4:12 PM
  • Hello BobbyBob711,

    Since your original issue has been solved, I would suggest you mark your reply to close the thread. For your new issue, you could try to refer to below link. If it does not fit your need or you have more detail requirements, please post a new thread for new issue. Thanks for understanding.

    Read from word document line by line

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 10, 2018 10:05 AM
  • Thanks for your reply Terry.  The suggested code Read from word document line by line uses Microsoft.Office.Interop.Word to read a .docx file line by line; the suggested code actually reads paragraph by paragraph. After some initial research I've learned that .docx file use the Open Xml format, and that the best reference to use is DocumentFormat.OpenXml

    Kind of learning as I go and this is where I am presently at. I shall provide usable updates as they come but if anyone has OpenXml pointers that can assist me with reading the text line by line that would be a big help.

    I am struggling with an output where each run being returned is kind of choppy, as if there are formatting tags preventing the line from being return as one continuous string. For anyone edification this code represents the tip of my spear.

    string cindy1 = @"K:\NicelyDone\Books\Cindy\Cindy1.docx";
    
    Package wordPackage = Package.Open(cindy1, FileMode.Open, FileAccess.Read);
    
                using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(wordPackage))
                {
                    Body body = wordDocument.MainDocumentPart.Document.Body;
                    using (FileStream fs = new FileStream(@"K:\Run.txt", FileMode.Create))
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        var parag = body.Elements<Paragraph>();
                        foreach (var para in parag)
                        {
                            foreach (var run in para.Elements<Run>())
                            {                            
                                foreach (var text in run.Elements<Text>())
                                {
                                    sw.WriteLine(run.InnerText);
                                }
                            }
                        }
                    }                    
                }
                wordPackage.Close();


    Robert Meyer

    • Marked as answer by BobbyBob711 Saturday, May 12, 2018 12:07 PM
    Thursday, May 10, 2018 6:27 PM