简体   繁体   English

使用OpenXML SDK用换行符替换docx文件上的文本(换行符)

[英]Using OpenXML SDK to replace text on a docx file with a line break (newline)

I am trying to use C# to replace a specific string of text on an entire DOCX file with a line break (newline). 我正在尝试使用C#替换整个 DOCX文件中的特定字符串,并使用换行符(换行符)。

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 (ie " ") instead of a line break. 问题:当我运行此代码时,输​​出DOCX文件的文本被替换为空格(即“”)而不是换行符。

How can I change this code to make this work? 如何更改此代码才能使其正常工作?

Try with a break . 试着休息一下 Check the example on this link. 查看此链接上的示例。 You just have to append a Break 你只需要附加一个Break

Paragraphs, smart tags, hyperlinks are all inside Run . 段落,智能标签,超链接都在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. 文本总是在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());
                        }
                    }
                }
            }
        }

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. 这样做会自动在两段之间添加换行符。

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

// 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: 文本:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM