Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Locate-first-table-after-selected-paragraph-text/Locate-first-table-after-selected-paragraph-text.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Locate_first_table_after_selected_paragraph_text</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>
<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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);
}
}
/// <summary>
/// Returns the first table after the selected paragraph text.
/// </summary>
/// <param name="body">The text body.</param>
/// <param name="index">The index of the selected paragraph.</param>
/// <returns></returns>
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;
}
}
}
Loading