@@ -7,18 +7,23 @@ class Program
77 {
88 static void Main ( string [ ] args )
99 {
10+ //Open the input Word document as a file stream
1011 using ( FileStream inputStream = new FileStream ( Path . GetFullPath ( @"../../../Data/Template.docx" ) , FileMode . Open , FileAccess . Read ) )
1112 {
13+ // Load the Word document
1214 using ( WordDocument document = new WordDocument ( inputStream , FormatType . Docx ) )
1315 {
1416 int i = 0 ;
17+ // Iterate through each section in the document
1518 foreach ( WSection section in document . Sections )
1619 {
20+ // Handle first page header / footer if enabled
1721 if ( section . PageSetup . DifferentFirstPage )
1822 {
1923 GenerateHTML ( section . HeadersFooters . FirstPageHeader , "FirstPageHeader_" + i + ".html" ) ;
2024 GenerateHTML ( section . HeadersFooters . FirstPageFooter , "FirstPageFooter_" + i + ".html" ) ;
2125 }
26+ // Handle even page header / footer if enabled
2227 else if ( section . PageSetup . DifferentOddAndEvenPages )
2328 {
2429 GenerateHTML ( section . HeadersFooters . EvenHeader , "EvenHeader_" + i + ".html" ) ;
@@ -39,27 +44,37 @@ static void Main(string[] args)
3944
4045 i ++ ;
4146 }
47+ // Save the remaining document body content as HTM
4248 using ( FileStream outputStream = new FileStream ( Path . GetFullPath ( @"../../../Output/TextBody.html" ) , FileMode . Create ) )
4349 {
4450 document . Save ( outputStream , FormatType . Html ) ;
4551 }
4652 }
47- }
53+ }
4854 }
55+ /// </summary>
56+ // Generates an HTML file from the given text body(header or footer).
57+ /// </summary>
58+ /// <param name="textBody">The text body (header/footer) to convert.</param>
59+ /// <param name="outputFile">The output HTML file name.</param>
60+
4961 private static void GenerateHTML ( WTextBody textBody , string outputFile )
5062 {
5163 string outputPath = Path . GetFullPath ( @"../../../Output/" ) ;
64+ // Check if the text body contains any content
5265 if ( textBody . ChildEntities . Count > 0 )
5366 {
67+ // Create a new Word document to hold extracted content
5468 WordDocument document = new WordDocument ( ) ;
5569 document . AddSection ( ) ;
70+ // Clone and add each entity from the source text body
5671 foreach ( Entity entity in textBody . ChildEntities )
5772 document . LastSection . Body . ChildEntities . Add ( entity . Clone ( ) ) ;
5873
74+ //Save the extracted content as an HTML file
5975 document . Save ( outputPath + outputFile , FormatType . Html ) ;
6076 }
6177 }
62-
6378 }
6479}
6580
0 commit comments