1+ using Syncfusion . DocIO ;
2+ using Syncfusion . DocIO . DLS ;
3+
4+ namespace Save_HTML_From_Word_Document
5+ {
6+ class Program
7+ {
8+ static void Main ( string [ ] args )
9+ {
10+ //Open the input Word document as a file stream
11+ using ( FileStream inputStream = new FileStream ( Path . GetFullPath ( @"../../../Data/Template.docx" ) , FileMode . Open , FileAccess . Read ) )
12+ {
13+ // Load the Word document
14+ using ( WordDocument document = new WordDocument ( inputStream , FormatType . Docx ) )
15+ {
16+ int i = 0 ;
17+ // Iterate through each section in the document
18+ foreach ( WSection section in document . Sections )
19+ {
20+ // Handle first page header / footer if enabled
21+ if ( section . PageSetup . DifferentFirstPage )
22+ {
23+ GenerateHTML ( section . HeadersFooters . FirstPageHeader , "FirstPageHeader_" + i + ".html" ) ;
24+ GenerateHTML ( section . HeadersFooters . FirstPageFooter , "FirstPageFooter_" + i + ".html" ) ;
25+ }
26+ // Handle even page header / footer if enabled
27+ else if ( section . PageSetup . DifferentOddAndEvenPages )
28+ {
29+ GenerateHTML ( section . HeadersFooters . EvenHeader , "EvenHeader_" + i + ".html" ) ;
30+ GenerateHTML ( section . HeadersFooters . EvenFooter , "EvenFooter_" + i + ".html" ) ;
31+
32+ }
33+ //This is the default header and footer
34+ GenerateHTML ( section . HeadersFooters . OddHeader , "OddHeader_" + i + ".html" ) ;
35+ GenerateHTML ( section . HeadersFooters . OddFooter , "OddFooter_" + i + ".html" ) ;
36+
37+ //After generating headers and footers, clear it
38+ section . HeadersFooters . FirstPageHeader . ChildEntities . Clear ( ) ;
39+ section . HeadersFooters . FirstPageFooter . ChildEntities . Clear ( ) ;
40+ section . HeadersFooters . EvenHeader . ChildEntities . Clear ( ) ;
41+ section . HeadersFooters . EvenFooter . ChildEntities . Clear ( ) ;
42+ section . HeadersFooters . OddHeader . ChildEntities . Clear ( ) ;
43+ section . HeadersFooters . OddFooter . ChildEntities . Clear ( ) ;
44+
45+ i ++ ;
46+ }
47+ // Save the remaining document body content as HTM
48+ using ( FileStream outputStream = new FileStream ( Path . GetFullPath ( @"../../../Output/TextBody.html" ) , FileMode . Create ) )
49+ {
50+ document . Save ( outputStream , FormatType . Html ) ;
51+ }
52+ }
53+ }
54+ }
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+
61+ private static void GenerateHTML ( WTextBody textBody , string outputFile )
62+ {
63+ string outputPath = Path . GetFullPath ( @"../../../Output/" ) ;
64+ // Check if the text body contains any content
65+ if ( textBody . ChildEntities . Count > 0 )
66+ {
67+ // Create a new Word document to hold extracted content
68+ WordDocument document = new WordDocument ( ) ;
69+ document . AddSection ( ) ;
70+ // Clone and add each entity from the source text body
71+ foreach ( Entity entity in textBody . ChildEntities )
72+ document . LastSection . Body . ChildEntities . Add ( entity . Clone ( ) ) ;
73+
74+ //Save the extracted content as an HTML file
75+ document . Save ( outputPath + outputFile , FormatType . Html ) ;
76+ }
77+ }
78+ }
79+ }
80+
0 commit comments