Skip to content

Commit 6ce0083

Browse files
Merge pull request #507 from SyncfusionExamples/ES-992086_loop_through_particular_table
ES-992086-Added DocIO Sample for iterate particular table by title in a word document
2 parents 5e418d4 + 45b9c37 commit 6ce0083

File tree

6 files changed

+86
-6
lines changed

6 files changed

+86
-6
lines changed

Bookmarks/Rename-bookmark/.NET/Rename-bookmark/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ static void Main(string[] args)
1212
//Opens an existing Word document.
1313
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
1414
{
15-
//Replace Bookmark name
16-
ReplaceBookmarkName(document, "Northwind", "New_Bookmark");
15+
//Rename Bookmark
16+
RenameBookmark(document, "Northwind", "New_Bookmark");
1717
//Creates file stream.
1818
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
1919
{
@@ -29,8 +29,8 @@ static void Main(string[] args)
2929
/// </summary>
3030
/// <param name="document">Input Word document.</param>
3131
/// <param name="existingBookmarkName">The name of the bookmark to replace.</param>
32-
/// <param name="replaceBookmarkName">The new name for the bookmark.</param>
33-
private static void ReplaceBookmarkName(WordDocument document, string existingBookmarkName, string replaceBookmarkName)
32+
/// <param name="newBookmarkName">The new name for the bookmark.</param>
33+
private static void RenameBookmark(WordDocument document, string existingBookmarkName, string newBookmarkName)
3434
{
3535
//Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name
3636
Bookmark bookmark = document.Bookmarks.FindByName(existingBookmarkName);
@@ -41,8 +41,8 @@ private static void ReplaceBookmarkName(WordDocument document, string existingBo
4141
int startIndex = -1;
4242
int endIndex = -1;
4343
// Create new bookmark start and end markers with the replacement name
44-
BookmarkStart newBookmarkStart = new BookmarkStart(document, replaceBookmarkName);
45-
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, replaceBookmarkName);
44+
BookmarkStart newBookmarkStart = new BookmarkStart(document, newBookmarkName);
45+
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, newBookmarkName);
4646

4747
// Determine the owner and index for the bookmark start.
4848
// The bookmark start may be inside a WParagraph (as a child entity)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Find-and-iterate-table-by-title/Find-and-iterate-table-by-title.csproj" />
3+
</Solution>
Binary file not shown.
Lines changed: 24 additions & 0 deletions
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>Find_and_iterate_table_by_title</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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.Drawing;
4+
5+
namespace Find_and_iterate_table_by_title
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+
//Opens an existing Word document.
14+
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
15+
{
16+
// Find the table with title.
17+
WTable table = document.FindItemByProperty(EntityType.Table, "Title", "Overview") as WTable;
18+
if (table != null)
19+
{
20+
// Iterate through the rows and cells of the table
21+
foreach (WTableRow row in table.Rows)
22+
{
23+
//Iterates through the cells of rows.
24+
foreach (WTableCell cell in row.Cells)
25+
{
26+
//Iterates through the paragraphs of the cell.
27+
foreach (WParagraph paragraph in cell.Paragraphs)
28+
{
29+
//When the paragraph contains text Panda then insert new text into paragraph.
30+
if (paragraph.Text.Contains("panda"))
31+
{
32+
WTextRange insertedText = paragraph.AppendText(" (Attributes)") as WTextRange;
33+
// Apply simple formatting only to the inserted text
34+
insertedText.CharacterFormat.Bold = true;
35+
insertedText.CharacterFormat.HighlightColor = Color.Yellow;
36+
}
37+
}
38+
}
39+
}
40+
}
41+
//Creates file stream.
42+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
43+
{
44+
//Saves the Word document to file stream.
45+
document.Save(outputStream, FormatType.Docx);
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+

0 commit comments

Comments
 (0)