|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | + |
| 4 | +namespace Replace_field_with_table |
| 5 | +{ |
| 6 | + internal class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + // Open the existing Word document using a FileStream. |
| 11 | + using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Data/InputDocument.docx"), FileMode.Open, FileAccess.Read)) |
| 12 | + { |
| 13 | + // Load the existing Word document. |
| 14 | + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) |
| 15 | + { |
| 16 | + // Find the first sequence field (SEQ field) in the document. |
| 17 | + WSeqField seqField = document.FindItemByProperty(EntityType.SeqField, "", "") as WSeqField; |
| 18 | + |
| 19 | + // Get the paragraph that contains the SEQ field. |
| 20 | + WParagraph paragraph = seqField.OwnerParagraph; |
| 21 | + |
| 22 | + // Get the index of the paragraph within the text body. |
| 23 | + int paraindex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); |
| 24 | + |
| 25 | + // Get the index of the SEQ field within the paragraph. |
| 26 | + int seqfieldIndex = paragraph.ChildEntities.IndexOf(seqField); |
| 27 | + |
| 28 | + // Clone the paragraph that contains the SEQ field. |
| 29 | + WParagraph clonedParagraph = seqField.OwnerParagraph.Clone() as WParagraph; |
| 30 | + |
| 31 | + // Remove all entities before the SEQ field index in the cloned paragraph. |
| 32 | + for (int i = seqfieldIndex; i >= 0; i--) |
| 33 | + { |
| 34 | + clonedParagraph.ChildEntities.RemoveAt(i); |
| 35 | + } |
| 36 | + |
| 37 | + // Remove all entities from the SEQ field index onward in the original paragraph. |
| 38 | + for (int j = paragraph.ChildEntities.Count - 1; j >= seqfieldIndex; j--) |
| 39 | + { |
| 40 | + paragraph.ChildEntities.RemoveAt(j); |
| 41 | + } |
| 42 | + |
| 43 | + // Generate a sample table. |
| 44 | + IWTable table = GetTable(); |
| 45 | + |
| 46 | + // Clone the generated table. |
| 47 | + IWTable table1 = table.Clone() as IWTable; |
| 48 | + |
| 49 | + // Insert the cloned table after the paragraph containing the SEQ field. |
| 50 | + paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 1, table1); |
| 51 | + |
| 52 | + // Insert the modified cloned paragraph after the inserted table. |
| 53 | + paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 2, clonedParagraph); |
| 54 | + |
| 55 | + // Save the modified document to a new file. |
| 56 | + using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"../../../Data/ResultDocument.docx"), FileMode.Create, FileAccess.Write)) |
| 57 | + { |
| 58 | + document.Save(docStream1, FormatType.Docx); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 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 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments