Skip to content

Commit

Permalink
898185-change-format-after-append-html
Browse files Browse the repository at this point in the history
  • Loading branch information
DharanyaSakthivel-SF4210 committed Jan 9, 2025
1 parent 73db5a3 commit 6b4e254
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
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}") = "Change-format-after-append-html", "Change-format-after-append-html\Change-format-after-append-html.csproj", "{5C44DC52-5C9E-4FE2-9F56-31ECB24E3B33}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5C44DC52-5C9E-4FE2-9F56-31ECB24E3B33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C44DC52-5C9E-4FE2-9F56-31ECB24E3B33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C44DC52-5C9E-4FE2-9F56-31ECB24E3B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C44DC52-5C9E-4FE2-9F56-31ECB24E3B33}.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>Change_format_after_append_html</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,52 @@
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;

namespace Change_format_after_append_html
{
internal class Program
{
static void Main(string[] args)
{
// Open the Word document.
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
{
// Access the first section of the document.
WSection section = document.Sections[0];

// Get the index of the last paragraph.
int paraIndex = section.Body.Paragraphs.Count - 1;

// Append HTML content to the document with formatting in <p> tag.
document.LastParagraph.AppendHTML("<p style='color:blue; font-weight:bold;'>The Giant</p><p style='color:green; font-style:italic;'>Panda</p>");

// Iterate through the paragraphs in the section's body.
for (int i = paraIndex; i < section.Body.ChildEntities.Count; i++)
{
// Get the paragraph and check if it's not null.
WParagraph paragraph = section.Body.ChildEntities[i] as WParagraph;
if (paragraph != null)
{
// Set the paragraph formatting spacing to 0.
paragraph.ParagraphFormat.BeforeSpacing = 0;
paragraph.ParagraphFormat.AfterSpacing = 0;

// Iterate through the items in the paragraph to chnage formatting.
foreach (var item in paragraph.ChildEntities)
{
if (item is WTextRange textRange)
textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.DarkGreen; // Change text color
}
}
}
// Save the modified document.
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
{
document.Save(outputStream, FormatType.Docx);
}
}
}
}
}
}

0 comments on commit 6b4e254

Please sign in to comment.