diff --git a/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph.sln b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph.sln new file mode 100644 index 000000000..181bd5e9f --- /dev/null +++ b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph.sln @@ -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}") = "Replace-text-in-list-paragraph", "Replace-text-in-list-paragraph\Replace-text-in-list-paragraph.csproj", "{CD5449C2-95ED-4D2B-8984-E2D4386503BA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CD5449C2-95ED-4D2B-8984-E2D4386503BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD5449C2-95ED-4D2B-8984-E2D4386503BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD5449C2-95ED-4D2B-8984-E2D4386503BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD5449C2-95ED-4D2B-8984-E2D4386503BA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8AAB32F5-BB2E-4918-B6CC-FF84105D3AC3} + EndGlobalSection +EndGlobal diff --git a/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Data/File.html b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Data/File.html new file mode 100644 index 000000000..c07ab1429 --- /dev/null +++ b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Data/File.html @@ -0,0 +1,7 @@ +<html> +<body> + <div style="font-family:calibri;color:#3C4D6F;"> + <p>Cycling Helmet</p> + </div> +</body> +</html> \ No newline at end of file diff --git a/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Data/Template.docx b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Data/Template.docx new file mode 100644 index 000000000..c0719baa5 Binary files /dev/null and b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Data/Template.docx differ diff --git a/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Output/.gitkeep b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Program.cs b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Program.cs new file mode 100644 index 000000000..64d8488ec --- /dev/null +++ b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Program.cs @@ -0,0 +1,66 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; + +using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) +{ + //Open the template Word document. + using (WordDocument document = new WordDocument(inputFileStream, FormatType.Automatic)) + { + string htmlFilePath = @"Data/File.html"; + //Check if the HTML content is valid. + bool isvalidHTML = document.LastSection.Body.IsValidXHTML(htmlFilePath, XHTMLValidationType.None); + if (isvalidHTML) + { + //Define the variable containing the text to search within the paragraph. + string variable = "Youth mountain bike"; + //Find the first occurrence of a particular text in the document + TextSelection textSelection = document.Find(variable, true, true); + //Get the found text as single text range + WTextRange textRange = textSelection.GetAsOneRange(); + // Get the paragraph containing the found text range + WParagraph paragraph = textRange.OwnerParagraph; + //Get the next sibling element of the current paragraph. + TextBodyItem nextSibling = paragraph.NextSibling as TextBodyItem; + //Get the index of the current paragraph within its parent text body. + int sourceIndex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); + //Clear all child entities within the paragraph. + paragraph.ChildEntities.Clear(); + //Get the list style name from the paragraph. + string listStyleName = paragraph.ListFormat.CurrentListStyle.Name; + //Get the current list level number. + int listLevelNum = paragraph.ListFormat.ListLevelNumber; + //Append HTML content from the specified file to the paragraph. + paragraph.AppendHTML(File.ReadAllText(Path.GetFullPath(htmlFilePath))); + //Reapply the original list style to the paragraph. + paragraph.ListFormat.ApplyStyle(listStyleName); + //Reapply the original list level number. + paragraph.ListFormat.ListLevelNumber = listLevelNum; + //Determine the index of the next sibling if it exists. + int nextSiblingIndex = nextSibling != null ? nextSibling.OwnerTextBody.ChildEntities.IndexOf(nextSibling) : -1; + //Apply the same list style to newly added paragraphs from the HTML content. + for (int k = sourceIndex; k < paragraph.OwnerTextBody.Count; k++) + { + //Stop applying the style if the next sibling is reached. + if (nextSiblingIndex != -1 && k == nextSiblingIndex) + { + break; + } + Entity entity = paragraph.OwnerTextBody.ChildEntities[k]; + //Apply the list style only if the entity is a paragraph. + if (entity is WParagraph) + { + (entity as WParagraph).ListFormat.ApplyStyle(listStyleName); + } + else + { + break; + } + } + } + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) + { + //Save the modified Word document to the output file stream. + document.Save(outputFileStream, FormatType.Docx); + } + } +} \ No newline at end of file diff --git a/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph.csproj b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph.csproj new file mode 100644 index 000000000..1e87d88a6 --- /dev/null +++ b/HTML-conversions/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph/Replace-text-in-list-paragraph.csproj @@ -0,0 +1,27 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <RootNamespace>Replace_text_in_list_paragraph</RootNamespace> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" /> + </ItemGroup> + + <ItemGroup> + <None Update="Data\File.html"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </None> + <None Update="Data\Template.docx"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </None> + <None Update="Output\.gitkeep"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </None> + </ItemGroup> + +</Project>