-
Notifications
You must be signed in to change notification settings - Fork 56
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
Apply-bold-formatting-to-the-content-between-placeholders #311
Merged
MohanaselvamJothi
merged 3 commits into
main
from
ES-904866-How-to-apply-bold-formatting-to-the-content-between-placeholders
Jan 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
| ||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.12.35309.182 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-bold-between-placeholder", "Apply-bold-between-placeholder\Apply-bold-between-placeholder.csproj", "{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{6CFD18EF-7889-4C29-A964-0FB48FBEEDFC}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {2127D849-B1E6-4BC4-8628-B8D6F0142835} | ||
EndGlobalSection | ||
EndGlobal |
24 changes: 24 additions & 0 deletions
24
...-between-placeholder/Apply-bold-between-placeholder/Apply-bold-between-placeholder.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_bold_between_placeholder</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.DocIO.NET" Version="*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="Data\Template.docx"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
<None Update="Output\.gitkeep"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
Binary file added
BIN
+75.9 KB
...-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Data/Template.docx
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Output/.gitkeep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
89 changes: 89 additions & 0 deletions
89
Find-and-Replace/Apply-bold-between-placeholder/Apply-bold-between-placeholder/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Syncfusion.DocIO.DLS; | ||
using Syncfusion.DocIO; | ||
using System.Text.RegularExpressions; | ||
|
||
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) | ||
{ | ||
// Create a WordDocument instance by loading the DOCX file from the file stream. | ||
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx)) | ||
{ | ||
// Apply bold formatting to specific text using a regular expression. | ||
ApplyBoldFormatting(document); | ||
|
||
// Save the modified document to an output file. | ||
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) | ||
{ | ||
// Save the modified Word document to the specified file path. | ||
document.Save(outputFileStream, FormatType.Docx); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Applies bold formatting to text enclosed in <b>...</b> tags in the Word document. | ||
/// </summary> | ||
static void ApplyBoldFormatting(WordDocument document) | ||
{ | ||
// Define a regular expression to find all occurrences of <b>...</b>. | ||
Regex regex = new Regex("<b>(.*?)</b>"); | ||
|
||
// Find all matches of the regex pattern in the document. | ||
TextSelection[] matches = document.FindAll(regex); | ||
|
||
// Iterate through each match found in the document. | ||
foreach (TextSelection match in matches) | ||
{ | ||
// Get the entire text range of the matched content. | ||
WTextRange textRange = match.GetAsOneRange(); | ||
|
||
// Extract the full text of the match. | ||
string fullText = textRange.Text; | ||
|
||
// Define the length of the opening tag <b>. | ||
int startTagLength = "<b>".Length; | ||
|
||
// Find the index of the closing tag </b>. | ||
int endTagIndex = fullText.LastIndexOf("</b>"); | ||
|
||
// Extract the opening tag, bold text, and closing tag as separate strings. | ||
string startTag = fullText.Substring(0, startTagLength); | ||
string boldText = fullText.Substring(startTagLength, endTagIndex - startTagLength); | ||
string endTag = fullText.Substring(endTagIndex); | ||
|
||
// Create new text ranges for each part (opening tag, bold text, and closing tag). | ||
WTextRange startTextRange = CreateTextRange(textRange, startTag); | ||
WTextRange boldTextRange = CreateTextRange(textRange, boldText); | ||
WTextRange endTextRange = CreateTextRange(textRange, endTag); | ||
|
||
// Apply bold formatting to the text range containing the bold text. | ||
boldTextRange.CharacterFormat.Bold = true; | ||
|
||
// Replace the original text range with the newly created text ranges in the paragraph. | ||
WParagraph paragraph = textRange.OwnerParagraph; | ||
|
||
// Get the index of the original text range within the paragraph. | ||
int index = paragraph.ChildEntities.IndexOf(textRange); | ||
|
||
// Remove the original text range from the paragraph. | ||
paragraph.ChildEntities.RemoveAt(index); | ||
|
||
// Insert the new text ranges (in order: closing tag, bold text, opening tag) into the paragraph. | ||
paragraph.ChildEntities.Insert(index, endTextRange); | ||
paragraph.ChildEntities.Insert(index, boldTextRange); | ||
paragraph.ChildEntities.Insert(index, startTextRange); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new text range with the specified text, copying formatting from the original range. | ||
/// </summary> | ||
static WTextRange CreateTextRange(WTextRange original, string text) | ||
{ | ||
// Clone the original text range to preserve its formatting. | ||
WTextRange newTextRange = original.Clone() as WTextRange; | ||
|
||
// Set the text for the new text range. | ||
newTextRange.Text = text; | ||
|
||
return newTextRange; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modify the template enclosed with only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified in template