aspose-The replace string cannot contain special or break characters.

报错代码:

1
2
3
string repStr = "*$a2$*";
string data = "阿阿阿阿阿柔顺发膜\r";
wh.doc.Range.Replace(repStr, data, false, false);

报错:

1
The replace string cannot contain special or break characters.

替换为:

1
2
3
WordHelper wh = new WordHelper(templateFile);
ReplaceHelper helper = new ReplaceHelper(wh.doc);
helper.Replace(repStr, data);
ReplaceHelper:

ReplaceHelper帮助类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
public class ReplaceHelper
{
public ReplaceHelper(Document doc)
{
mDoc = doc;
}

/// <summary>
/// Replaces old text with new. New text can contain any special characters.
/// Old text cannot contain special charactes.
/// </summary>
/// <param name="oldText"></param>
/// <param name="newText"></param>
public void Replace(string oldText, string newText)
{
mDoc.Range.Replace(new Regex(Regex.Escape(oldText)), new ReplaceEvaluatorFindAndInsertText(newText), false);
}

public class ReplaceEvaluatorFindAndInsertText : IReplacingCallback
{
public ReplaceEvaluatorFindAndInsertText(string text)

{
mText = text;
}

/// <summary>
/// This method is called by the Aspose.Words find and replace engine for each match.
/// This method replaces the match string, even if it spans multiple runs.</summary>
/// <param name="e"></param>
/// <returns></returns>
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 removing.
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);
}

// Create Document Buidler and insert text.
DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);

builder.MoveTo((Run)runs[runs.Count - 1]);

builder.Write(mText);

// Now remove all runs in the sequence.
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;
}

/// <summary>
/// Splits text of the specified run into two runs.
/// Inserts the new run just after the specified run.</summary>
/// <param name="run"></param>
/// <param name="position"></param>
/// <returns></returns>
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;
}

private string mText;
}

private Document mDoc;
}