ReplaceCallback sometimes doesn't replace searchpattern

We’re using a template and want to replace a given placeholder by a list. Sometimes only the first char of the placeholder has been removed, sometimes multiple chars, sometimes it’s working.The issue is hard to reproduce but seems to be related to the template. For a given template, the results seems to always be the same. If I edit the placeholder (remove & add it again) it may suddenly work as expected or remove another amount of chars from the placeholder.

public void CreatePdf(Stream templateStream, Stream pdfResultStream, Content content)
{
Document doc = new Document(templateStream);

if (content.DocumentNames.Any())
{
    string documentNames = string.Join("|", content.DocumentNames);
    doc.Range.Replace("{documentList"}, documentNames, new FindReplaceOptions { MatchCase = false, FindWholeWordsOnly = false, ReplacingCallback = new ReplaceEvaluatorReplaceWithList() });
}

doc.Save(pdfResultStream, SaveFormat.Pdf);

}

public class ReplaceEvaluatorReplaceWithList : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs e)
{
Node node = e.MatchNode;
Document document = (Document)node.Document;

    DocumentBuilder builder = new DocumentBuilder(document);
    builder.MoveTo(node);
    builder.ListFormat.List = document.Lists.Add(ListTemplate.BulletCircle);
    builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.List;

    string[] listItems = e.Replacement.Split('|');
    for (int i = 0; i < listItems.Length - 1; ++i)
    {
        builder.Writeln(listItems[i]);
    }

    builder.Write(listItems[listItems.Length - 1]);

    node.Remove();
    return ReplaceAction.Skip;
}

}

Please see attached Files for more details (template, code, pdf).
samples.zip (33.0 KB)

@joerg.bieri,

Thanks for your inquiry. Please use the following modified code to get the desired output. If you still face problem, please ZIP and attach your input and expected output documents here for testing. We will investigate the issue on our side and provide you more information.

private class ReplaceEvaluatorReplaceWithList : IReplacingCallback
{
    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// </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);

        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.ListFormat.List = builder.Document.Lists.Add(ListTemplate.BulletCircle);
        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.List;

        string[] listItems = e.Replacement.Split('|');
        for (int i = 0; i < listItems.Length - 1; ++i)
        {
            builder.Writeln(listItems[i]);
        }

        builder.Write(listItems[listItems.Length - 1]);

        // Now remove the matched node
        foreach (Run run in runs)
            run.Remove();

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;
    }

    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;
    }
}

@tahir.manzoor
Thanks for your fast response, this did the trick.