|
| 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 | + // Get the paragraph that contains the SEQ field. |
| 19 | + WParagraph paragraph = seqField.OwnerParagraph; |
| 20 | + // Get the index of the paragraph within the text body. |
| 21 | + int paraindex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); |
| 22 | + // Get the index of the SEQ field within the paragraph. |
| 23 | + int seqfieldIndex = paragraph.ChildEntities.IndexOf(seqField); |
| 24 | + // Clone the paragraph that contains the SEQ field. |
| 25 | + WParagraph clonedParagraph = seqField.OwnerParagraph.Clone() as WParagraph; |
| 26 | + // Remove all entities before the SEQ field index in the cloned paragraph. |
| 27 | + for (int i = seqfieldIndex; i >= 0; i--) |
| 28 | + { |
| 29 | + clonedParagraph.ChildEntities.RemoveAt(i); |
| 30 | + } |
| 31 | + // Remove all entities from the SEQ field index onward in the original paragraph. |
| 32 | + for (int j = paragraph.ChildEntities.Count - 1; j >= seqfieldIndex; j--) |
| 33 | + { |
| 34 | + paragraph.ChildEntities.RemoveAt(j); |
| 35 | + } |
| 36 | + |
| 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"); |
| 47 | + |
| 48 | + // Clone the generated table. |
| 49 | + IWTable table1 = table.Clone() as IWTable; |
| 50 | + // Insert the cloned table after the paragraph containing the SEQ field. |
| 51 | + paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 1, table1); |
| 52 | + // Insert the modified cloned paragraph after the inserted table. |
| 53 | + paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 2, clonedParagraph); |
| 54 | + // Save the modified document to a new file. |
| 55 | + using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write)) |
| 56 | + { |
| 57 | + document.Save(docStream1, FormatType.Docx); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments