内容批量替换问题

怎么将Word内容,批量替换指定的内容为换行回车?
或者搜索出指定的内容,在指定内容的前、后增加换行回车?
例:
原始内容:将文档内容中的【End】替换,增加【End】换行回车。
替换后:将文档内容中的
【End】
替换,增加
【End】
换行回车

@dhzhao2016,

感谢您的询问。 为了确保及时和准确的回复,请在此处邮寄并附上以下资源以进行测试:

  • 您输入的Word文档
  • 您的预期文件。 我们将调查您期望的文档的结构,以了解您希望如何生成最终输出。 您可以使用Microsoft Word创建预期的文档。

然后,我们将通过使用Aspose.Words为您提供代码以实现相同效果。

您好:附件中包含两个文档(“输入文档.docx”是原始有内容的文档;“预期文档.docx”是希望通过aspose.word处理之后的格式),具体实现说明:
1、将“输入文档.docx”文档内容中,所有的“【#】”符号都另起一行(参照“预期文档.docx”)。或者如果能获取到“【#】”符号的定位,前、后都加回车也可以。
2、我在读取“输入文档.docx”内容时,想逐个读取文档的Node(doc.FirstSection.Body.ChildNodes),可是“A、2【#】”部分内容没有单独获取到。我对比了一下,“A、2【#】”这部分内容的回车,和其他内容的回车不一样,但是人工是查看不出来的,所以aspose.word能否查找出这样的回车符并全部替换。

不知道我的问题是否描述清楚了,十分感谢您的回复和解答,谢谢!测试文档.zip (31.9 KB)

@dhzhao2016,

我们正在处理您的查询,并会很快回复您。

@dhzhao2016,

请尝试使用以下代码:

private class MyReplaceEvaluator : IReplacingCallback
{
    /// <summary>
    /// This is called during a replace operation each time a match is found.
    /// This method appends a number to the match string and returns it as a replacement string.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further highlighting.
        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
        {
            runs.Add(currentNode);
            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
            do
            {
                currentNode = currentNode.NextSibling;
            }
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
        }

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {
            SplitRun((Run)currentNode, remainingLength);
            runs.Add(currentNode);
        }

        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo((Run)runs[0]);
        builder.Writeln();
        Node n = ((Run)runs[runs.Count - 1]).NextSibling;
        if (n != null)
        {
            builder.MoveTo(n);
            builder.Writeln();
        }

        return ReplaceAction.Skip;
    }

    /// <summary>
    /// Splits text of the specified run into two runs.
    /// Inserts the new run just after the specified run.
    /// </summary>
    private static Run SplitRun(Run run, int position)
    {
        Run afterRun = (Run)run.Clone(true);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring(0, position);
        run.ParentNode.InsertAfter(afterRun, run);
        return afterRun;
    }
}

Document doc = new Document(MyDir + @"ab\input.docx");

FindReplaceOptions opts = new FindReplaceOptions(FindReplaceDirection.Backward);
opts.ReplacingCallback = new MyReplaceEvaluator();

doc.Range.Replace(new Regex("【#】"), "", opts);

doc.Save(MyDir + @"ab\18.3.docx");