@@ -8,112 +8,56 @@ internal class Program
88 static void Main ( string [ ] args )
99 {
1010 // Open the existing Word document using a FileStream.
11- using ( FileStream docStream = new FileStream ( Path . GetFullPath ( @"../../../ Data/InputDocument.docx" ) , FileMode . Open , FileAccess . Read ) )
11+ using ( FileStream docStream = new FileStream ( Path . GetFullPath ( @"Data/InputDocument.docx" ) , FileMode . Open , FileAccess . Read ) )
1212 {
1313 // Load the existing Word document.
1414 using ( WordDocument document = new WordDocument ( docStream , FormatType . Docx ) )
1515 {
1616 // Find the first sequence field (SEQ field) in the document.
1717 WSeqField seqField = document . FindItemByProperty ( EntityType . SeqField , "" , "" ) as WSeqField ;
18-
1918 // Get the paragraph that contains the SEQ field.
2019 WParagraph paragraph = seqField . OwnerParagraph ;
21-
2220 // Get the index of the paragraph within the text body.
2321 int paraindex = paragraph . OwnerTextBody . ChildEntities . IndexOf ( paragraph ) ;
24-
2522 // Get the index of the SEQ field within the paragraph.
2623 int seqfieldIndex = paragraph . ChildEntities . IndexOf ( seqField ) ;
27-
2824 // Clone the paragraph that contains the SEQ field.
2925 WParagraph clonedParagraph = seqField . OwnerParagraph . Clone ( ) as WParagraph ;
30-
3126 // Remove all entities before the SEQ field index in the cloned paragraph.
3227 for ( int i = seqfieldIndex ; i >= 0 ; i -- )
3328 {
3429 clonedParagraph . ChildEntities . RemoveAt ( i ) ;
3530 }
36-
3731 // Remove all entities from the SEQ field index onward in the original paragraph.
3832 for ( int j = paragraph . ChildEntities . Count - 1 ; j >= seqfieldIndex ; j -- )
3933 {
4034 paragraph . ChildEntities . RemoveAt ( j ) ;
4135 }
4236
43- // Generate a sample table.
44- IWTable table = GetTable ( ) ;
37+ //Create a new table
38+ IWTable table = new WTable ( document ) ;
39+ table . ResetCells ( 3 , 2 ) ;
40+ table . Rows [ 0 ] . Cells [ 0 ] . AddParagraph ( ) . AppendText ( "Sno" ) ;
41+ table . Rows [ 0 ] . Cells [ 1 ] . AddParagraph ( ) . AppendText ( "Product" ) ;
42+ table . Rows [ 0 ] . IsHeader = true ;
43+ table . Rows [ 1 ] . Cells [ 0 ] . AddParagraph ( ) . AppendText ( "1." ) ;
44+ table . Rows [ 1 ] . Cells [ 1 ] . AddParagraph ( ) . AppendText ( "Essential DocIO" ) ;
45+ table . Rows [ 2 ] . Cells [ 0 ] . AddParagraph ( ) . AppendText ( "2." ) ;
46+ table . Rows [ 2 ] . Cells [ 1 ] . AddParagraph ( ) . AppendText ( "Essential Pdf" ) ;
4547
4648 // Clone the generated table.
4749 IWTable table1 = table . Clone ( ) as IWTable ;
48-
4950 // Insert the cloned table after the paragraph containing the SEQ field.
5051 paragraph . OwnerTextBody . ChildEntities . Insert ( paraindex + 1 , table1 ) ;
51-
5252 // Insert the modified cloned paragraph after the inserted table.
5353 paragraph . OwnerTextBody . ChildEntities . Insert ( paraindex + 2 , clonedParagraph ) ;
54-
5554 // Save the modified document to a new file.
56- using ( FileStream docStream1 = new FileStream ( Path . GetFullPath ( @"../../../Data/ResultDocument .docx" ) , FileMode . Create , FileAccess . Write ) )
55+ using ( FileStream docStream1 = new FileStream ( Path . GetFullPath ( @"Output/Result .docx" ) , FileMode . Create , FileAccess . Write ) )
5756 {
5857 document . Save ( docStream1 , FormatType . Docx ) ;
5958 }
6059 }
6160 }
6261 }
63-
64- // Method to generate a sample table.
65- static IWTable GetTable ( )
66- {
67- // Creates a new Word document.
68- WordDocument document = new WordDocument ( ) ;
69-
70- // Adds a section into the Word document.
71- IWSection section = document . AddSection ( ) ;
72-
73- // Adds a paragraph with the text "Price Details" in bold and Arial font.
74- IWTextRange textRange = section . AddParagraph ( ) . AppendText ( "Price Details" ) ;
75- textRange . CharacterFormat . FontName = "Arial" ;
76- textRange . CharacterFormat . FontSize = 12 ;
77- textRange . CharacterFormat . Bold = true ;
78-
79- // Adds an empty paragraph (for spacing).
80- section . AddParagraph ( ) ;
81-
82- // Adds a new table with 3 rows and 2 columns.
83- IWTable table = section . AddTable ( ) ;
84- table . ResetCells ( 3 , 2 ) ;
85-
86- // Adds the column headers to the first row.
87- textRange = table [ 0 , 0 ] . AddParagraph ( ) . AppendText ( "Item" ) ;
88- textRange . CharacterFormat . FontName = "Arial" ;
89- textRange . CharacterFormat . FontSize = 12 ;
90- textRange . CharacterFormat . Bold = true ;
91-
92- textRange = table [ 0 , 1 ] . AddParagraph ( ) . AppendText ( "Price($)" ) ;
93- textRange . CharacterFormat . FontName = "Arial" ;
94- textRange . CharacterFormat . FontSize = 12 ;
95- textRange . CharacterFormat . Bold = true ;
96-
97- // Adds the first item and its price to the second row.
98- textRange = table [ 1 , 0 ] . AddParagraph ( ) . AppendText ( "Cycle 1" ) ;
99- textRange . CharacterFormat . FontName = "Arial" ;
100- textRange . CharacterFormat . FontSize = 10 ;
101-
102- textRange = table [ 1 , 1 ] . AddParagraph ( ) . AppendText ( "500" ) ;
103- textRange . CharacterFormat . FontName = "Arial" ;
104- textRange . CharacterFormat . FontSize = 10 ;
105-
106- // Adds the second item and its price to the third row.
107- textRange = table [ 2 , 0 ] . AddParagraph ( ) . AppendText ( "Cycle 2" ) ;
108- textRange . CharacterFormat . FontName = "Arial" ;
109- textRange . CharacterFormat . FontSize = 10 ;
110-
111- textRange = table [ 2 , 1 ] . AddParagraph ( ) . AppendText ( "300" ) ;
112- textRange . CharacterFormat . FontName = "Arial" ;
113- textRange . CharacterFormat . FontSize = 10 ;
114-
115- // Returns the generated table.
116- return table ;
117- }
11862 }
11963}
0 commit comments