Skip to content

Commit 84bd640

Browse files
Merge pull request #373 from SyncfusionExamples/ES-895943-Keep-paragraphs-together-in-table
ES-895943- Add the sample Keep-paragraphs-together-in-table
2 parents b748824 + 07b9478 commit 84bd640

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Keep-paragraphs-together-in-table", "Keep-paragraphs-together-in-table\Keep-paragraphs-together-in-table.csproj", "{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EA22BFD7-89F9-4F72-BECC-743931FBDFA6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {ADDC6FEE-8718-4B00-B091-6C4DC0CBF62A}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Keep_paragraphs_together_in_table</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.Licensing;
4+
5+
namespace Keep_paragraphs_together_in_table
6+
{
7+
internal class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
12+
{
13+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
14+
{
15+
//Find all tables by EntityType in Word document.
16+
List<Entity> tables = document.FindAllItemsByProperty(EntityType.Table, null, null);
17+
18+
for (int i = 0; i < tables.Count; i++)
19+
{
20+
WTable table = tables[i] as WTable;
21+
// Apply "Keep with Next" for all paragraphs inside the table
22+
SetKeepTogetherForTable(table);
23+
}
24+
25+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
26+
{
27+
document.Save(outputStream, FormatType.Docx);
28+
}
29+
}
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Ensures that all paragraphs inside a table stay together.
35+
/// </summary>
36+
private static void SetKeepTogetherForTable(WTable table)
37+
{
38+
foreach (WTableRow row in table.Rows)
39+
{
40+
foreach (WTableCell cell in row.Cells)
41+
{
42+
foreach (Entity item in cell.ChildEntities)
43+
{
44+
if (item is WTable nestedTable)
45+
{
46+
// Recursively process nested tables
47+
SetKeepTogetherForTable(nestedTable);
48+
}
49+
else if (item is WParagraph paragraph)
50+
{
51+
// Ensure paragraphs stay together
52+
paragraph.ParagraphFormat.KeepFollow = true; // Keep with next
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)