Skip to content

Commit f9cf8f5

Browse files
901973-Modified the input Word document
1 parent c83c74d commit f9cf8f5

File tree

2 files changed

+4
-14
lines changed
  • Find-and-Replace/Find-and-apply-bold-to-replaced-content/.NET/Find-and-apply-bold-to-replaced-content

2 files changed

+4
-14
lines changed

Find-and-Replace/Find-and-apply-bold-to-replaced-content/.NET/Find-and-apply-bold-to-replaced-content/Program.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,39 @@ static void Main(string[] args)
1515
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
1616
{
1717
// Find the text pattern using a regular expression.
18-
TextSelection selection = document.Find(new Regex("Adventure Works Cycles"));
19-
18+
TextSelection selection = document.Find(new Regex("^<<([^>]*)>>"));
2019
// Get the selected text as a single text range.
2120
WTextRange textRange = selection.GetAsOneRange();
22-
2321
// Get the text body and paragraph of selected text.
2422
WTextBody body = textRange.OwnerParagraph.OwnerTextBody;
2523
WParagraph paragraph = textRange.OwnerParagraph;
26-
2724
// Get the index of the paragraph within the text body.
2825
int paraIndex = body.ChildEntities.IndexOf(paragraph);
29-
3026
// Replace the selected text with the replace content.
31-
document.Replace("Adventure Works Cycles", "Company name", true, true);
32-
27+
document.Replace(selection.SelectedText, "Adventure Works Cycles", true, true);
3328
// Iterate the body from the replaced paragraph.
3429
for (int i = paraIndex; i < body.Count; i++)
3530
{
3631
// Access each paragraph in the body.
3732
WParagraph para = body.ChildEntities[i] as WParagraph;
38-
3933
// Search for the word within the current paragraph.
40-
TextSelection replacedSelection = para.Find("Company", true, false);
41-
34+
TextSelection replacedSelection = para.Find("Works", true, false);
4235
if (replacedSelection != null)
4336
{
4437
// Get the selected text as a single text range.
4538
WTextRange replacedTextRange = replacedSelection.GetAsOneRange();
46-
4739
// Apply bold formatting to the text range.
4840
replacedTextRange.CharacterFormat.Bold = true;
4941
break;
5042
}
5143
}
52-
5344
// Save the modified document to the specified output path.
5445
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
5546
{
5647
document.Save(outputStream, FormatType.Docx);
5748
}
5849
}
5950
}
60-
6151
}
6252
}
63-
}
53+
}

0 commit comments

Comments
 (0)