Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How insert paragraph on new line (\r\n) #40

Open
windofny opened this issue Oct 3, 2017 · 0 comments
Open

How insert paragraph on new line (\r\n) #40

windofny opened this issue Oct 3, 2017 · 0 comments

Comments

@windofny
Copy link

windofny commented Oct 3, 2017

What if I need to insert Paragraph on new line ?

Early here was issue #2 , and was addes code:

public static void ReplaceNewLinesWithBreaks(XElement xElem)
        {
            if (xElem == null) return;

            var textWithBreaks = xElem.Descendants(W.t).Where(t => t.Value.Contains("\r\n"));
            foreach (var textWithBreak in textWithBreaks)
            {
                var text = textWithBreak.Value;
                var split = text.Replace("\r\n", "\n").Split(new[] { "\n" }, StringSplitOptions.None);
                textWithBreak.Value = string.Empty;
                foreach (var s in split)
                {
                    textWithBreak.Add(new XElement(W.t, s));
                    textWithBreak.Add(new XElement(W.br));
                }
                textWithBreak.Descendants(W.br).Last().Remove();
            }
        }

But when it insert br - it don't copy style for element (like padding). How can I add real paragraph ? I tried make like this, but it's breaking document:

 public static void ReplaceNewLinesWithBreaks(XElement xElem)
        {
            if (xElem == null) return;

            var textWithBreaks = xElem.Descendants(W.t).Where(t => t.Value.Contains("\r\n"));
            foreach (var textWithBreak in textWithBreaks)
            {
                var text = textWithBreak.Value;
                var split = text.Replace("\r\n", "\n").Split(new[] { "\n" }, StringSplitOptions.None);
                textWithBreak.Value = string.Empty;
                foreach (var s in split)
                {
                    xElem.Add(new XElement(W.p, new XElement(W.r, new XElement(W.t, s))));      //here changed code              
                }                                
            }
        }

Maybe I can get some true parent element, where I can add those new Paragraphs ?

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant