Hi,
we are trying to replace tags with html text (which is a content control) but we get the following error:
‘The reference node is not a child of this node.’
the code we use is:
doc.Range.Replace(lookupString, "", new FindReplaceOptions(new ReplaceWithHtmlEvaluator(lookupString, input.Value, isContentControlRequired: input.IsContentControlRequired, isBlockContentControl: true)));
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
var document = e.MatchNode.Document as Document;
DocumentBuilder builder = new DocumentBuilder(document);
var runToReplace = (Run)runs[runs.Count - 1];
builder.MoveTo(runToReplace);
if (ChangeToWhiteFont)
{
foreach (Run run in runs)
{
run.Font.Color = Color.White;
}
return ReplaceAction.Skip;
}
else if (IsContentControlRequired)
{
var tag = Searchstring.Replace("##", "").Replace("$$", "").Replace("**", "");
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(document, SdtType.RichText, IsBlockContentControl ? MarkupLevel.Block : MarkupLevel.Inline);
sdtRichText.RemoveAllChildren();
sdtRichText.Tag = tag;
sdtRichText.IsShowingPlaceholderText = false;
if (IsBlockContentControl)
{
Run run = new Run(document);
run.Text = "";
Paragraph para = new Paragraph(document);
para.AppendChild(run);
sdtRichText.AppendChild(para);
// getting the error in below line
document.FirstSection.Body.InsertAfter(sdtRichText, runToReplace.ParentNode);
builder.MoveTo(sdtRichText.FirstChild);
builder.InsertHtml(ReplaceWith, HtmlInsertOptions.UseBuilderFormatting);
if (builder.CurrentParagraph != null && builder.CurrentParagraph.ToString(SaveFormat.Text).Trim() == "")
builder.CurrentParagraph.Remove();
// formatting the content to match the searchstring's styles
NodeCollection runNodes = sdtRichText.GetChildNodes(NodeType.Run, true);
foreach (Run rn in runNodes)
{
rn.Font.Color = runToReplace.Font.Color;
rn.Font.Name = runToReplace.Font.Name;
rn.Font.Style = runToReplace.Font.Style;
rn.Font.Size = runToReplace.Font.Size;
rn.Font.Italic = runToReplace.Font.Italic;
rn.Font.Bold = runToReplace.Font.Bold;
rn.Font.StrikeThrough = runToReplace.Font.StrikeThrough;
}
if (runToReplace.ParentNode != null)
{
runToReplace.ParentNode.Remove();
}
}
else
{
runToReplace.Text = ReplaceWith;
sdtRichText.AppendChild(runToReplace.Clone(true));
sdtRichText.LockContentControl = true;
sdtRichText.LockContents = true;
builder.InsertNode(sdtRichText);
}
}
else
{
builder.InsertHtml(ReplaceWith, HtmlInsertOptions.UseBuilderFormatting);
if (builder.CurrentParagraph != null && Searchstring.Contains(builder.CurrentParagraph.ToString(SaveFormat.Text).Trim()))
builder.CurrentParagraph.Remove();
}
foreach (Run run in runs)
{
run.Remove();
}
return ReplaceAction.Skip;
}