Hi Team,
Let’s take an example such that a documents consists of below text
- Hello
- World
How to write a regex pattern to match and create a content control? I need to match both Hello and World and it should create a content control
string pattern = @"1\s*Hello\s*2\s*World";
var options = new FindReplaceOptions {
ReplacingCallback = new ReplaceWithContentControlHandler(doc, "MyContentControl"),
MatchCase = false,
FindWholeWordsOnly = false,
SmartParagraphBreakReplacement = true
};
var patern = new Regex(pattern);
doc.Range.Replace(new Regex(pattern, RegexOptions.IgnoreCase), "", options);
class ReplaceWithContentControlHandler : IReplacingCallback {
private readonly Document _doc;
private readonly string _title;
private readonly string _tag;
public ReplaceWithContentControlHandler(Document doc, string title) {
_doc = doc;
_title = title;
}
public ReplaceAction Replacing(ReplacingArgs e) {
// Create a new RichText content control
StructuredDocumentTag sdt = new StructuredDocumentTag(_doc, SdtType.RichText, MarkupLevel.Inline) {
LockContentControl = false,
LockContents = false,
Title = "Clause",
IsShowingPlaceholderText = false
};
sdt.RemoveAllChildren();
// Create a new Run node and add it to the content control
Run run1 = new Run(_doc, e.Match.Value);
sdt.AppendChild(run1);
// Insert the content control into the document
Node currentNode = e.MatchNode;
// Check if the current node is a paragraph
if (currentNode.NodeType == NodeType.Paragraph) {
Paragraph currentParagraph = (Paragraph)currentNode;
// If the match is at the beginning of the paragraph
if (e.MatchOffset == 0) {
// Create a new Run node and add it to the content control
Run run = new Run(_doc, e.Match.Value);
sdt.AppendChild(run);
// Insert the content control at the beginning of the paragraph
currentParagraph.InsertBefore(sdt, currentParagraph.FirstChild);
}
else {
// If the match is not at the beginning, you need to split the paragraph text
// Create two runs: one before the match and one for the match
string textBeforeMatch = currentParagraph.GetText().Substring(0, e.MatchOffset);
string matchText = e.Match.Value;
// Clear the paragraph
currentParagraph.RemoveAllChildren();
// Add the text before the match as a new run
if (!string.IsNullOrEmpty(textBeforeMatch)) {
Run runBefore = new Run(_doc, textBeforeMatch);
currentParagraph.AppendChild(runBefore);
}
// Create a run for the match text and add it to the content control
Run runMatch = new Run(_doc, matchText);
sdt.AppendChild(runMatch);
// Insert the content control into the paragraph
currentParagraph.AppendChild(sdt);
}
}
else if (currentNode.NodeType == NodeType.Run) {
Run currentRun = (Run)currentNode;
CompositeNode parent = currentRun.ParentNode;
// Split the run and insert the content control at the split point
if (e.MatchOffset > 0) {
var totalLength = currentRun.Text.Length;
Run beforeRun = (Run)currentRun.Clone(true);
beforeRun.Text = currentRun.Text.Substring(0, e.MatchOffset);
parent.InsertBefore(beforeRun, currentRun);
Run afterRun = (Run)currentRun.Clone(true);
var sdtTextLength = sdt.GetText().Length;
var replaceLength = e.MatchOffset + sdtTextLength;
afterRun.Text = currentRun.Text.Substring(replaceLength);
parent.InsertAfter(afterRun, currentRun);
parent.InsertAfter(sdt, beforeRun);
parent.RemoveChild(currentRun);
}
else {
parent.RemoveAllChildren();
parent.AppendChild(sdt);
//parent.InsertBefore(sdt, currentRun);
//parent.RemoveChild(currentRun);
}
}
return ReplaceAction.Skip;
}
}
Is it possible to create it? I have the above code but it’s unable to match text between two bullets