I am using the code below to replace hyphens with nonbreaking hyphens to address some business rules required formatting.
// Replace hyphens with non breaking hyphens
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
foreach (Run run in runs)
{
// only replace for endorsementids (which contain two spaces together and hyphens)
if (run.Text.Contains("-") && run.Text.Contains(" "))
{
run.Text = run.Text.Replace("-", "" + ControlChar.NonBreakingHyphenChar);
}
}
The code works, but it iterates through the entire document. I know the only places that need to have the replacement done are always in the first two sections of the document. Also, the text is always within a form field.
Is there away to limit the code to run only with certain sections or only on text with a form field?