hi,
we are facing an issue with replacing content inside block content control. here is an example,
title: $$placeholder1$$ $$placeholder2$$ $$placeholder3$$ $$placeholder4$$
in the above, entire line is in a content control, and we iterate over the placeholders to replace. so when we replace the second time, the placeholders are not there. only the first one is replaced.
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
ReplaceWith = ReplaceWith == null ? "" : ReplaceWith;
// 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);
// below line is causing the issue
runToReplace.ParentNode.ParentNode.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;
}
one workaround we tried, is having a paragraph break between each placeholder but we want the title and placeholders to be in same line
another workaround we tried is, if the line has multiple placeholders, we switch it to inline content control
please advise, thank you