diff --git a/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content.sln b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content.sln new file mode 100644 index 000000000..51a56fbc8 --- /dev/null +++ b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content.sln @@ -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 diff --git a/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Apply-style-to-bookmark-content.csproj b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Apply-style-to-bookmark-content.csproj new file mode 100644 index 000000000..e03e6fa2a --- /dev/null +++ b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Apply-style-to-bookmark-content.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Apply_style_to_bookmark_content + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Data/Template.docx b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Data/Template.docx new file mode 100644 index 000000000..7c0bb1251 Binary files /dev/null and b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Data/Template.docx differ diff --git a/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Output/.gitkeep b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Program.cs b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Program.cs new file mode 100644 index 000000000..1b88c3c30 --- /dev/null +++ b/Bookmarks/Apply-style-to-bookmark-content/.NET/Apply-style-to-bookmark-content/Program.cs @@ -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); +} + +/// +/// Iterates all sections, headers, and footers in the given document. +/// +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); + } +} + +/// +/// Iterates all entities (paragraphs, tables, block content controls) within a WTextBody. +/// +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; + } + } +} + +/// +/// Iterates all rows and cells in a table, processing each cell's text body. +/// +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); + } + } +} + +/// +/// Iterates all paragraph items and applies the specified style formatting. +/// +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; + } + } +} \ No newline at end of file