Skip to content
Merged
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
12 changes: 6 additions & 6 deletions Bookmarks/Rename-bookmark/.NET/Rename-bookmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ static void Main(string[] args)
//Opens an existing Word document.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
{
//Replace Bookmark name
ReplaceBookmarkName(document, "Northwind", "New_Bookmark");
//Rename Bookmark
RenameBookmark(document, "Northwind", "New_Bookmark");
//Creates file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
Expand All @@ -29,8 +29,8 @@ static void Main(string[] args)
/// </summary>
/// <param name="document">Input Word document.</param>
/// <param name="existingBookmarkName">The name of the bookmark to replace.</param>
/// <param name="replaceBookmarkName">The new name for the bookmark.</param>
private static void ReplaceBookmarkName(WordDocument document, string existingBookmarkName, string replaceBookmarkName)
/// <param name="newBookmarkName">The new name for the bookmark.</param>
private static void RenameBookmark(WordDocument document, string existingBookmarkName, string newBookmarkName)
{
//Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name
Bookmark bookmark = document.Bookmarks.FindByName(existingBookmarkName);
Expand All @@ -41,8 +41,8 @@ private static void ReplaceBookmarkName(WordDocument document, string existingBo
int startIndex = -1;
int endIndex = -1;
// Create new bookmark start and end markers with the replacement name
BookmarkStart newBookmarkStart = new BookmarkStart(document, replaceBookmarkName);
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, replaceBookmarkName);
BookmarkStart newBookmarkStart = new BookmarkStart(document, newBookmarkName);
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, newBookmarkName);

// Determine the owner and index for the bookmark start.
// The bookmark start may be inside a WParagraph (as a child entity)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Find-and-iterate-table-by-title/Find-and-iterate-table-by-title.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Find_and_iterate_table_by_title</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Drawing;

namespace Find_and_iterate_table_by_title
{
internal class Program
{
static void Main(string[] args)
{
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
//Opens an existing Word document.
using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic))
{
// Find the table with title.
WTable table = document.FindItemByProperty(EntityType.Table, "Title", "Overview") as WTable;
if (table != null)
{
// Iterate through the rows and cells of the table
foreach (WTableRow row in table.Rows)
{
//Iterates through the cells of rows.
foreach (WTableCell cell in row.Cells)
{
//Iterates through the paragraphs of the cell.
foreach (WParagraph paragraph in cell.Paragraphs)
{
//When the paragraph contains text Panda then insert new text into paragraph.
if (paragraph.Text.Contains("panda"))
{
WTextRange insertedText = paragraph.AppendText(" (Attributes)") as WTextRange;
// Apply simple formatting only to the inserted text
insertedText.CharacterFormat.Bold = true;
insertedText.CharacterFormat.HighlightColor = Color.Yellow;
}
}
}
}
}
//Creates file stream.
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
document.Save(outputStream, FormatType.Docx);
}
}
}
}
}
}

Loading