1- //Open an existing document.
2- using Syncfusion . DocIO . DLS ;
1+ using Syncfusion . DocIO . DLS ;
32using Syncfusion . DocIO ;
43
54//Open an existing Word document from the specified file path.
6- FileStream fileStreamPath = new FileStream ( Path . GetFullPath ( @"Data/Input.docx" ) , FileMode . Open , FileAccess . Read ) ;
7- //Create a new instance of the WordDocument class and load the document from the FileStream.
8- WordDocument document = new WordDocument ( fileStreamPath , FormatType . Docx ) ;
9- //Retrieve the first section of the Word document.
10- WSection firstSection = document . Sections [ 0 ] ;
11- //Iterate through each section in the Word document starting from the second section.
12- for ( int index = 1 ; index < document . Sections . Count ; index ++ )
5+ using ( FileStream fileStreamPath = new FileStream ( Path . GetFullPath ( @"Data/Input.docx" ) , FileMode . Open , FileAccess . Read ) )
136{
14- //Copy the headers and footers from the first section to the current section.
15- UpdateHeaderFooter ( firstSection , document . Sections [ index ] ) ;
7+ //Create a new instance of the WordDocument class and load the document from the FileStream.
8+ using ( WordDocument document = new WordDocument ( fileStreamPath , FormatType . Docx ) )
9+ {
10+ //Retrieve the first section of the Word document.
11+ WSection firstSection = document . Sections [ 0 ] ;
12+ //Iterate through each section in the Word document starting from the second section.
13+ for ( int index = 1 ; index < document . Sections . Count ; index ++ )
14+ {
15+ //Copy the headers and footers from the first section to the current section.
16+ UpdateHeaderFooter ( firstSection , document . Sections [ index ] ) ;
17+ }
18+ //Remove the first section from the document.
19+ document . Sections . RemoveAt ( 0 ) ;
20+ //Create a FileStream to save the modified document to a new file at the specified path.
21+ using ( FileStream stream = new FileStream ( Path . GetFullPath ( @"Output/Output.docx" ) , FileMode . Create , FileAccess . Write ) )
22+ {
23+ //Save the Word document to the stream in DOCX format.
24+ document . Save ( stream , FormatType . Docx ) ;
25+ }
26+ }
1627}
17- //Remove the first section from the document.
18- document . Sections . RemoveAt ( 0 ) ;
19- //Create a FileStream to save the modified document to a new file at the specified path.
20- FileStream stream = new FileStream ( Path . GetFullPath ( @"Output/Output.docx" ) , FileMode . Create , FileAccess . Write ) ;
21- //Save the Word document to the stream in DOCX format.
22- document . Save ( stream , FormatType . Docx ) ;
23- //Close the WordDocument instance to release the resources.
24- document . Close ( ) ;
25-
2628
2729///<summary>
2830///Copy the source section header and footer to destination section
@@ -31,19 +33,18 @@ void UpdateHeaderFooter(WSection sourceSection, WSection destinationSection)
3133{
3234 //clear the destination section header and footer
3335 ClearHeaderFooter ( destinationSection ) ;
34-
3536 //Add Headers
3637 for ( int j = 0 ; j < sourceSection . HeadersFooters . Header . ChildEntities . Count ; j ++ )
3738 {
3839 destinationSection . HeadersFooters . Header . ChildEntities . Add ( sourceSection . HeadersFooters . Header . ChildEntities [ j ] . Clone ( ) ) ;
3940 }
40-
4141 //Add Footers
4242 for ( int j = 0 ; j < sourceSection . HeadersFooters . Footer . ChildEntities . Count ; j ++ )
4343 {
4444 destinationSection . HeadersFooters . Footer . ChildEntities . Add ( sourceSection . HeadersFooters . Footer . ChildEntities [ j ] . Clone ( ) ) ;
4545 }
4646}
47+
4748///<summary>
4849///Clear all header and footer for the section
4950/// </summary>
0 commit comments