@Chilke You can check whether after replacement the target paragraph is empty and if so remove it. Please see the following test code and implementation of IReplacingCallback:
// Some items are commented out to demonstrate the concept works as expected.
Dictionary<string, string> tags = new Dictionary<string, string>()
{
{"sepworker-notifyby","sepworker-notifyby"},
{"worker-full-legal-name","worker-full-legal-name"},
{"worker-employeeID","worker-employeeID"},
{"worker-job-title","worker-job-title"},
{"worker-supervisory-org","worker-supervisory-org"},
{"worker-division","worker-division"},
{"sepworker-last-day","sepworker-last-day"},
{"TEXT-Availability","TEXT-Availability"},
{"sev-PaymentAmount","sev-PaymentAmount"},
{"sev-SevWeeks","sev-SevWeeks"},
//{"Text-SepBonus-P1","Text-SepBonus-P1"},
//{"sev-lumpSum","sev-lumpSum"},
//{"Text-SepBonus-P2","Text-SepBonus-P2"},
//{"Text-Outplacement","Text-Outplacement"},
{"N","N"},
{"Text-PaidPTO","Text-PaidPTO"},
{"sev-AgreePeriod","sev-AgreePeriod"},
{"sepworker-NoteJobTitle","sepworker-NoteJobTitle"},
{"sepworker-NoteName","sepworker-NoteName"},
{"sepworker-NoteAddress","sepworker-NoteAddress"},
{"TEXT-RIFGroupOver40-ExhibitA","TEXT-RIFGroupOver40-ExhibitA"},
{"TEXT-RIFGroupOver40-AddLang","TEXT-RIFGroupOver40-AddLang"},
{"sepworker-Notifyplus45","sepworker-Notifyplus45"}
};
Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions options = new FindReplaceOptions
{
ReplacingCallback = new TagReplacer(tags),
Direction = FindReplaceDirection.Backward // <----- NOTE: replace direction is changed to backward.
};
doc.Range.Replace(new Regex(@"\{\{(.+?)\}\}"), "", options);
doc.Save(@"C:\Temp\out.docx");
private class TagReplacer : IReplacingCallback
{
public TagReplacer(Dictionary<string, string> tags)
{
mTags = tags;
}
public ReplaceAction Replacing(ReplacingArgs args)
{
string key = args.Match.Groups[1].Value;
string value = mTags.ContainsKey(key) ? mTags[key] : "";
Document doc = (Document)args.MatchNode.Document;
List<Run> matchedRuns = GetMatchedRuns(args);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(matchedRuns.First());
builder.Write(value);
// Delete matched runs
foreach (Run run in matchedRuns)
run.Remove();
// Check whether current paragraph is empty, if so remove it.
Paragraph currentPara = builder.CurrentParagraph;
if (string.IsNullOrEmpty(currentPara.ToString(SaveFormat.Text).Trim()) &&
currentPara.GetChildNodes(NodeType.Shape, true).Count == 0)
currentPara.Remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
protected static List<Run> GetMatchedRuns(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 deleting.
List<Run> runs = new List<Run>();
// 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((Run)currentNode);
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((Run)currentNode);
}
return runs;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
run.ParentNode.InsertAfter(afterRun, run);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
return afterRun;
}
private Dictionary<string, string> mTags;
}