Using OpenXML SDK to replace text on a docx file with a line break (newline)
Asked Answered
L

4

3

I am trying to use C# to replace a specific string of text on an entire DOCX file with a line break (newline).

The string of text that I am searching for could be in a paragraph or in a table in the file.

I am currently using the code below to replace text.

using (WordprocessingDocument doc = WordprocessingDocument.Open("yourdoc.docx", true))
{
  var body = doc.MainDocumentPart.Document.Body;

  foreach (var text in body.Descendants<Text>())
  {
    if (text.Text.Contains("##Text1##"))
    {
      text.Text = text.Text.Replace("##Text1##", Environment.NewLine);
    }
  }
}

ISSUE: When I run this code, the output DOCX file has the text replaced with a space (i.e. " ") instead of a line break.

How can I change this code to make this work?

Levitus answered 10/10, 2014 at 20:37 Comment(0)
C
2

Try with a break. Check the example on this link. You just have to append a Break

Paragraphs, smart tags, hyperlinks are all inside Run. So maybe you could try this approach. To change the text inside a table, you will have to use this approach. Again the text is always inside a Run.

If you are saying that the replace is only replacing for an empty string, i would try this:

using (WordprocessingDocument doc =
                WordprocessingDocument.Open(@"yourpath\testdocument.docx", true))
        {
            var body = doc.MainDocumentPart.Document.Body;
            var paras = body.Elements<Paragraph>();

            foreach (var para in paras)
            {
                foreach (var run in para.Elements<Run>())
                {
                    foreach (var text in run.Elements<Text>())
                    {
                        if (text.Text.Contains("text-to-replace"))
                        {
                            text.Text = text.Text.Replace("text-to-replace", "");
            run.AppendChild(new Break());
                        }
                    }
                }
            }
        }
Choreodrama answered 10/10, 2014 at 20:51 Comment(3)
Thanks for looking into this. Using run.AppendChild(new Break()); will probably create a new line. But how can I have that put into the document only where it is supposed to replace the text?Levitus
The Text.Replace() function only accepts two strings I believeLevitus
This only replaces single-run text. Text may be split up in multiple runs.Neslund
I
1

Instead of adding a line break, try making two paragraphs, one before the "text to be replaced" and one after. Doing that will automatically add a line break between two paragraphs.

Irrigate answered 10/12, 2014 at 22:10 Comment(0)
P
0

text.Parent.Append(new DocumentFormat.OpenXml.Wordprocessing.Break());

Particularize answered 12/6, 2016 at 8:42 Comment(1)
Hi Arun, welcome to SO. Can you please edit this answer to explain why this code fixes the problem, and how it works? Code only answers are discouraged here.Obscenity
M
0
// Open a WordprocessingDocument for editing using the filepath.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
{
 // Assign a reference to the existing document body.
 Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

 //itenerate throught text
 foreach (var text in body.Descendants<Text>())
 {
   text.Parent.Append(new Text("Text:"));
   text.Parent.Append(new Break());
   text.Parent.Append(new Text("Text"));
 }
}

This will return:

Text:
Text:

Molybdate answered 18/4, 2017 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.