Skip to content

Commit 4f6bb60

Browse files
Merge pull request #250 from VijayadharshiniMathiyalagan/ES-881100-How-to-find-and-replace-text-in-heading-paragraphs-in-a-Word-document
ES-881100 Added sample for replace text in heading paragraph
2 parents a957056 + 6a98b80 commit 4f6bb60

File tree

2 files changed

+10
-11
lines changed
  • Find-item-in-word-document/Replace-text-heading-paragraphs/.NET/Replace-text-heading-paragraphs

2 files changed

+10
-11
lines changed

Find-item-in-word-document/Replace-text-heading-paragraphs/.NET/Replace-text-heading-paragraphs/Program.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-

2-
3-
using Microsoft.VisualBasic.FileIO;
4-
using Syncfusion.DocIO;
1+
using Syncfusion.DocIO;
52
using Syncfusion.DocIO.DLS;
63

74
namespace Replace_text_heading_paragraphs
@@ -10,24 +7,26 @@ class Program
107
{
118
static void Main(string[] args)
129
{
13-
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
10+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
1411
{
15-
//Opens an existing Word document.
16-
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
12+
//Opens the input Word document.
13+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Automatic))
1714
{
1815
for (int headingLevel = 1; headingLevel < 10; headingLevel++)
1916
{
20-
//Find headings based on the levels and endnote by paragraph in Word document.
17+
//Get all heading paragraphs based on the heading style level.
2118
List<Entity> headings = document.FindAllItemsByProperty(EntityType.Paragraph, "StyleName", "Heading " + headingLevel);
22-
//Replace the headings with text.
19+
//Iterate through all headings in the list.
2320
for (int index = 0; index < headings.Count; index++)
2421
{
22+
//Cast the current heading to WParagraph.
2523
WParagraph paragraph = headings[index] as WParagraph;
24+
//Remove all child elements from the paragraph.
2625
paragraph.ChildEntities.Clear();
27-
paragraph.AppendText("Replaced Heading"+headingLevel+" text");
26+
//Add new text to replace the heading content.
27+
paragraph.AppendText("Replaced Heading" + headingLevel + " text");
2828
}
2929
}
30-
//Creates file stream.
3130
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
3231
{
3332
//Saves the Word document to file stream.

0 commit comments

Comments
 (0)