Skip to content
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
Show file tree
Hide file tree
Changes from 1 commit
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,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
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 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,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>");
Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified in template


// 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;
}
Loading