diff --git a/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table.sln b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table.sln new file mode 100644 index 000000000..537b63293 --- /dev/null +++ b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34622.214 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-field-with-table", "Replace-field-with-table\Replace-field-with-table.csproj", "{AD2A243A-F1F9-410E-BE4E-592839D52919}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AD2A243A-F1F9-410E-BE4E-592839D52919}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AD2A243A-F1F9-410E-BE4E-592839D52919}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AD2A243A-F1F9-410E-BE4E-592839D52919}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AD2A243A-F1F9-410E-BE4E-592839D52919}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5A0B4C55-E8AA-4F27-BB36-8B562758FB60} + EndGlobalSection +EndGlobal diff --git a/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Data/InputDocument.docx b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Data/InputDocument.docx new file mode 100644 index 000000000..69fb84c24 Binary files /dev/null and b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Data/InputDocument.docx differ diff --git a/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Output/.gitkeep b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Program.cs b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Program.cs new file mode 100644 index 000000000..e40ba1954 --- /dev/null +++ b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Program.cs @@ -0,0 +1,63 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; + +namespace Replace_field_with_table +{ + internal class Program + { + static void Main(string[] args) + { + // Open the existing Word document using a FileStream. + using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/InputDocument.docx"), FileMode.Open, FileAccess.Read)) + { + // Load the existing Word document. + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) + { + // Find the first sequence field (SEQ field) in the document. + WSeqField seqField = document.FindItemByProperty(EntityType.SeqField, "", "") as WSeqField; + // Get the paragraph that contains the SEQ field. + WParagraph paragraph = seqField.OwnerParagraph; + // Get the index of the paragraph within the text body. + int paraindex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); + // Get the index of the SEQ field within the paragraph. + int seqfieldIndex = paragraph.ChildEntities.IndexOf(seqField); + // Clone the paragraph that contains the SEQ field. + WParagraph clonedParagraph = seqField.OwnerParagraph.Clone() as WParagraph; + // Remove all entities before the SEQ field index in the cloned paragraph. + for (int i = seqfieldIndex; i >= 0; i--) + { + clonedParagraph.ChildEntities.RemoveAt(i); + } + // Remove all entities from the SEQ field index onward in the original paragraph. + for (int j = paragraph.ChildEntities.Count - 1; j >= seqfieldIndex; j--) + { + paragraph.ChildEntities.RemoveAt(j); + } + + //Create a new table + IWTable table = new WTable(document); + table.ResetCells(3, 2); + table.Rows[0].Cells[0].AddParagraph().AppendText("Sno"); + table.Rows[0].Cells[1].AddParagraph().AppendText("Product"); + table.Rows[0].IsHeader = true; + table.Rows[1].Cells[0].AddParagraph().AppendText("1."); + table.Rows[1].Cells[1].AddParagraph().AppendText("Essential DocIO"); + table.Rows[2].Cells[0].AddParagraph().AppendText("2."); + table.Rows[2].Cells[1].AddParagraph().AppendText("Essential Pdf"); + + // Clone the generated table. + IWTable table1 = table.Clone() as IWTable; + // Insert the cloned table after the paragraph containing the SEQ field. + paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 1, table1); + // Insert the modified cloned paragraph after the inserted table. + paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 2, clonedParagraph); + // Save the modified document to a new file. + using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write)) + { + document.Save(docStream1, FormatType.Docx); + } + } + } + } + } +} diff --git a/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Replace-field-with-table.csproj b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Replace-field-with-table.csproj new file mode 100644 index 000000000..08f1890e8 --- /dev/null +++ b/Find-and-Replace/Replace-field-with-table/.NET/Replace-field-with-table/Replace-field-with-table.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Replace_field_with_table + enable + enable + + + + + + + + + Always + + + Always + + + +