Hi Team,
HarishKumar.A:
Hi Team,I am trying to replace a matching text with another text using range. replace. I am not using any document builder but directly giving the text in the replace method as belowdoc_MngLetter.Range.Replace("[Insert Action required from option below]", "I note that the above breach has been rectified, no further action required."+ ControlChar.LineBreak + ControlChar.LineBreak + "[Section/Regulation number] – [Description]", false, false);[Insert Action required from option below] - This is the text which needs to be matched"I note that the above breach has been rectified, no further action required."+ ControlChar.LineBreak + ControlChar.LineBreak + "[Section/Regulation number] – [Description]" -- This is the text which will be replacing the above text ([Insert Action required from option below]).Issue here is i am trying to add bold font to [Section/Regulation number] – [Description] but it is not applying . I tried using " [Section/Regulation number] – [Description] .Can you provide me the solution for this and also i need to add numbering.
Hi Tahir,
Document doc = new Document(MyDir + "in.docx");
String[] newcontents = { "New contents ", "Bold contents" };
Regex regex = new Regex("Insert Action required from option below", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, new ReplaceEvaluatorTest001(newcontents), false);
doc.Save(MyDir + "Out.docx");
private class ReplaceEvaluatorTest001 : IReplacingCallback
{
String[] newcontents;
public ReplaceEvaluatorTest001(String[] contents)
{
newcontents = contents;
}
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);
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
DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
builder.MoveTo((Run)runs[runs.Count - 1]);
builder.Write(newcontents[0]);
builder.Font.Bold = true;
builder.Write(newcontents[1]);
builder.Font.Bold = false;
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;
}
private static 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;
}
}
Hi Tahir,
Hi Tahir,
Hi Harish,
- Your input Word document. If Management Letter.docx is your input document, no need to share it.
- Please attach the output Word file that shows the undesired behavior.
- Please attach your target Word document showing the desired behavior. You can use Microsoft Word to create your target Word document. We will investigate as to how you are expecting your final document be generated like.
- Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.
Hi
Hi Harish,
Regarding replacing text with list items, you need to use the same code example shared here. Please write your new code at the place of highlighted code snippet.