diff --git a/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text.slnx b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text.slnx new file mode 100644 index 00000000..7ba71fb3 --- /dev/null +++ b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Data/Template.docx b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Data/Template.docx new file mode 100644 index 00000000..a8a167c4 Binary files /dev/null and b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Data/Template.docx differ diff --git a/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Locate-first-table-after-selected-paragraph-text.csproj b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Locate-first-table-after-selected-paragraph-text.csproj new file mode 100644 index 00000000..304f877e --- /dev/null +++ b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Locate-first-table-after-selected-paragraph-text.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + Locate_first_table_after_selected_paragraph_text + enable + enable + + + + + + + Always + + + Always + + + \ No newline at end of file diff --git a/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Output/.gitkeep b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Output/output.docx b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Output/output.docx new file mode 100644 index 00000000..02f0e63c Binary files /dev/null and b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Output/output.docx differ diff --git a/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Program.cs b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Program.cs new file mode 100644 index 00000000..50db5c4e --- /dev/null +++ b/Tables/Locate-first-table-after-selected-paragraph-text/.NET/Locate-first-table-after-selected-paragraph-text/Program.cs @@ -0,0 +1,52 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +namespace Locate_first_table_after_selected_paragraph_text +{ + class Program + { + static void Main(string[] args) + { + using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../../Data/Template.docx"))) + { + //Finds the first occurrence of a particular text in the document + TextSelection textSelection = document.Find("About the Suppliers", false, true); + //Gets the found text as single text range + WTextRange textRange = textSelection.GetAsOneRange(); + //Gets the owner paragraph of the selected text + WParagraph paragraph = textRange.OwnerParagraph; + //Gets the owner textbody of the selected paragraph + WTextBody textbody = paragraph.OwnerTextBody; + //Gets the index of the selected paragraph + int paraIndex = textbody.ChildEntities.IndexOf(paragraph); + + //Gets the first table after the selected paragraph text + WTable firstTableAfterSelection = GetFirstTableAfterSelection(textbody, paraIndex); + if (firstTableAfterSelection != null) + { + //To do anything; + } + //Save the Word document + document.Save(Path.GetFullPath(@"../../../Output/output.docx"), FormatType.Docx); + } + } + /// + /// Returns the first table after the selected paragraph text. + /// + /// The text body. + /// The index of the selected paragraph. + /// + static WTable GetFirstTableAfterSelection(WTextBody body, int paraIndex) + { + //Iterates through the body items to get the first table after the selected paragraph text. + for (int i = paraIndex + 1; i < body.ChildEntities.Count; i++) + { + if (body.ChildEntities[i] is WTable) + { + return body.ChildEntities[i] as WTable; + } + } + //Returns null, if there is no table after the selected paragraph text in the Word document. + return null; + } + } +} \ No newline at end of file