I’d like to replace “Page %%PAGE%% of %%NUMPAGES%%” to “Page {PAGE * MERGEFORMAT} of {NUMPAGES * MERGEFORMAT}” at my document. E.g. convert all %%FIELD%% into {FIELD}.
So I took a ReplaceTextWithFields sample and did it like this.
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new ReplaceTextWithFieldHandler();
doc.Range.Replace(new Regex(@"%%(.*?)%%"), "", options);
public class ReplaceTextWithFieldHandler : IReplacingCallback
{
public ReplaceTextWithFieldHandler()
{
}
public ReplaceAction Replacing(ReplacingArgs args)
{
ArrayList runs = FindAndSplitMatchRuns(args);
// Create DocumentBuilder which is used to insert the field.
DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);
builder.MoveTo((Run)runs[runs.Count - 1]);
// Insert the field into the document using the specified field type and the match text as the field name.
// If the fields you are inserting do not require this extra parameter then it can be removed from the string below.
builder.InsertField(args.Match.Groups[1].ToString());
// 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>
/// Finds and splits the match runs and returns them in an ArrayList.
/// </summary>
public ArrayList FindAndSplitMatchRuns(ReplacingArgs args)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = args.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 (args.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, args.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 = args.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);
}
return runs;
}
/// <summary>
/// Splits text of the specified run into two runs.
/// Inserts the new run just after the specified run.
/// </summary>
private 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;
}
}
And I get an exception at SplitRun.
If I split the text into 2 lines, it does work fine:
“Page %%PAGE%%
of %%NUMPAGES%%”
So I have a feeling that all this splitting somehow corrupts the flow. But I’m not sure why is it even necessary, and what to do to fix it…