Skip to content

Commit f4f0580

Browse files
Merge pull request #278 from Suriya-Balamurugan/ES-894931-Split-table-by-bookmark
Added sample to split a table in a Word document using a bookmark between rows
2 parents 9957978 + 81702f5 commit f4f0580

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-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 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Split-table-by-bookmark", "Split-table-by-bookmark\Split-table-by-bookmark.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace Split_table_by_bookmark
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
13+
{
14+
//Open an existing Word document.
15+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
16+
{
17+
//Get all the bookmarkstart in the cell.
18+
//If bookmark mentioned anywhere in table "tr" in file level, DocIO parsed inside cell only.
19+
List<Entity> bookmarkStart = document.FindAllItemsByProperty(EntityType.BookmarkStart, "OwnerParagraph.IsInCell", true.ToString());
20+
//Find start and end row based on bookmark start.
21+
//Then split the table.
22+
for (int i = 0; i < bookmarkStart.Count; i++)
23+
{
24+
BookmarkStart start = bookmarkStart[i] as BookmarkStart;
25+
WTableRow startRow = GetOwnerRow(start);
26+
WTable table = startRow.Owner as WTable;
27+
WTableRow endRow;
28+
29+
#region Find start and end row to split based on bookmark start
30+
//If there is any bookmark start further in table,
31+
//then get previous row of bookmark start.
32+
if (i < bookmarkStart.Count - 1)
33+
{
34+
//Get the owner row of next bookmark start.
35+
WTableRow ownerRow = GetOwnerRow(bookmarkStart[i + 1] as BookmarkStart);
36+
//Get the previous row.
37+
endRow = ownerRow.PreviousSibling as WTableRow;
38+
}
39+
//If there is no further bookmark start in table, consider last row as end of splitted table.
40+
else
41+
endRow = (startRow.Owner as WTable).LastRow;
42+
#endregion
43+
44+
#region Split table
45+
//Split the table from start row to end row.
46+
WTable splittedTable = SplitTable(table, startRow.GetRowIndex(), endRow.GetRowIndex());
47+
//Get the owner of table.
48+
WTextBody ownerBody = table.OwnerTextBody;
49+
int indexToInsert = ownerBody.ChildEntities.IndexOf(table) + i * 2 + 1;
50+
//Insert paragraph to differentiate two tables.
51+
//As per Microsoft Word behavior, if two tables without any paragraph between them, it will be treated as one table.
52+
WParagraph paragraph = new WParagraph(document);
53+
ownerBody.ChildEntities.Insert(indexToInsert, paragraph);
54+
//Add splitted table, next to the paragraph.
55+
indexToInsert++;
56+
ownerBody.ChildEntities.Insert(indexToInsert, splittedTable);
57+
#endregion
58+
}
59+
//Create file stream.
60+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite))
61+
{
62+
//Save the Word document to file stream.
63+
document.Save(outputFileStream, FormatType.Docx);
64+
}
65+
}
66+
}
67+
}
68+
#region Helper methods
69+
/// <summary>
70+
/// Split a table from the bookmarks start row to the end row.
71+
/// </summary>
72+
/// <param name="table">Table to split.</param>
73+
/// <param name="rowStartIndex">Start row index.</param>
74+
/// <param name="rowEndIndex">End row index.</param>
75+
static WTable SplitTable(WTable table, int rowStartIndex, int rowEndIndex)
76+
{
77+
//Clone the table.
78+
WTable clonedTable = table.Clone();
79+
//Table should have atleast 1 row and 1 cell.
80+
clonedTable.ResetCells(1, 1);
81+
82+
//Clone the rows from owner table to new table one by one.
83+
for (int i = rowStartIndex; i <= rowEndIndex; i++)
84+
{
85+
clonedTable.Rows.Add(table.Rows[i].Clone());
86+
}
87+
//Remove the first row from cloned table.
88+
clonedTable.Rows.RemoveAt(0);
89+
90+
//Delete those row from owner table.
91+
for (int i = rowEndIndex; i >= rowStartIndex; i--)
92+
{
93+
table.Rows.RemoveAt(i);
94+
}
95+
return clonedTable;
96+
}
97+
/// <summary>
98+
/// Get the owner row of bookmark start.
99+
/// </summary>
100+
/// <param name="bookmarkStart">Bookmark start to get owner row.</param>
101+
static WTableRow GetOwnerRow(BookmarkStart bookmarkStart)
102+
{
103+
if (bookmarkStart.OwnerParagraph.IsInCell)
104+
{
105+
return (bookmarkStart.OwnerParagraph.OwnerTextBody as WTableCell).OwnerRow;
106+
}
107+
return null;
108+
}
109+
#endregion
110+
}
111+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Split_table_by_bookmark</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)