1-
2-
3- using Microsoft . VisualBasic . FileIO ;
4- using Syncfusion . DocIO ;
1+ using Syncfusion . DocIO ;
52using Syncfusion . DocIO . DLS ;
63
74namespace 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