Skip to content

ES-957516- Add the sample Apply-style-to-Bookmark-content #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-style-to-bookmark-content", "Apply-style-to-bookmark-content\Apply-style-to-bookmark-content.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
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>Apply_style_to_bookmark_content</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 @@

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


// Declare a variable to hold the custom character style used for formatting bookmark content.
WCharacterStyle style;

// Load the Word document.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
// Navigate to the bookmark named "Tiny_Cubes".
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark("Tiny_Cubes");

// Extract the content inside the bookmark as a separate Word document.
WordDocument bookmarkContent = bookmarkNavigator.GetBookmarkContent().GetAsWordDocument();

// Retrieve the character style named "TinyCube" from the style collection.
IStyleCollection styleCollection = document.Styles;
style = styleCollection.FindByName("TinyCube") as WCharacterStyle;

// Apply the retrieved style to all elements in the extracted bookmark content.
IterateDocumentElements(bookmarkContent);

// Create a WordDocumentPart from the modified bookmark content.
WordDocumentPart wordDocumentPart = new WordDocumentPart(bookmarkContent);

// Replace the original bookmark content with the styled content.
bookmarkNavigator.ReplaceContent(wordDocumentPart);

// Save the updated document to a new file.
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
}

/// <summary>
/// Iterates all sections, headers, and footers in the given document.
/// </summary>
void IterateDocumentElements(WordDocument document)
{
foreach (WSection section in document.Sections)
{
// Process the main body of the section.
IterateTextBody(section.Body);

// Process the header and footer (only OddHeader and OddFooter here).
WHeadersFooters headersFooters = section.HeadersFooters;
IterateTextBody(headersFooters.OddHeader);
IterateTextBody(headersFooters.OddFooter);
}
}

/// <summary>
/// Iterates all entities (paragraphs, tables, block content controls) within a WTextBody.
/// </summary>
void IterateTextBody(WTextBody textBody)
{
for (int i = 0; i < textBody.ChildEntities.Count; i++)
{
IEntity bodyItemEntity = textBody.ChildEntities[i];

switch (bodyItemEntity.EntityType)
{
case EntityType.Paragraph:
// Process paragraph items (text, fields, etc.)
IterateParagraph((bodyItemEntity as WParagraph).Items);
break;

case EntityType.Table:
// Recursively process each cell in the table.
IterateTable(bodyItemEntity as WTable);
break;

case EntityType.BlockContentControl:
// Recursively process the text body within a block content control.
IterateTextBody((bodyItemEntity as BlockContentControl).TextBody);
break;
}
}
}

/// <summary>
/// Iterates all rows and cells in a table, processing each cell's text body.
/// </summary>
void IterateTable(WTable table)
{
foreach (WTableRow row in table.Rows)
{
foreach (WTableCell cell in row.Cells)
{
// Each cell is a TextBody; reuse IterateTextBody to process its content.
IterateTextBody(cell);
}
}
}

/// <summary>
/// Iterates all paragraph items and applies the specified style formatting.
/// </summary>
void IterateParagraph(ParagraphItemCollection paraItems)
{
for (int i = 0; i < paraItems.Count; i++)
{
Entity entity = paraItems[i];

switch (entity.EntityType)
{
case EntityType.TextRange:
// Apply the character style to the text range.
(entity as WTextRange).ApplyCharacterFormat(style.CharacterFormat);
break;

case EntityType.Field:
// Apply the character style to the field.
(entity as WField).ApplyCharacterFormat(style.CharacterFormat);
break;

case EntityType.TextBox:
// Recursively process the contents of the textbox.
IterateTextBody((entity as WTextBox).TextBoxBody);
break;

case EntityType.Shape:
// Recursively process the contents of the shape.
IterateTextBody((entity as Shape).TextBody);
break;

case EntityType.InlineContentControl:
// Recursively process the paragraph items within the inline content control.
IterateParagraph((entity as InlineContentControl).ParagraphItems);
break;
}
}
}
Loading