Skip to content

Commit ddc171a

Browse files
Added sample to change the font for specific content in a Word document
1 parent 9f2d2d7 commit ddc171a

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Change-specific-content-font", "Change-specific-content-font\Change-specific-content-font.csproj", "{17B1609A-9006-4997-82C7-3A2FEEA82657}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {28C9FA54-8845-4B02-86AD-5B7AA7EEC783}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Change_specific_content_font</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Change_specific_content_font
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
11+
{
12+
//Open an existing Word document.
13+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
14+
{
15+
//Find all the paragraphs based on specific style name.
16+
List<Entity> paragraphs = document.FindAllItemsByProperty(EntityType.Paragraph, "StyleName", "List Paragraph");
17+
//Iterate through each paragraph.
18+
foreach (WParagraph paragraph in paragraphs)
19+
{
20+
//Iterate through each child items in the paragraph.
21+
foreach (Entity childItem in paragraph.ChildEntities)
22+
{
23+
//Check if entity is WTextRange.
24+
if (childItem is WTextRange)
25+
{
26+
WTextRange textRange = childItem as WTextRange;
27+
//Change the font name for the text range.
28+
textRange.CharacterFormat.FontName = "Algerian";
29+
}
30+
}
31+
}
32+
//Create output file stream.
33+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
34+
{
35+
//Save the modified Word document.
36+
document.Save(outputStream, FormatType.Docx);
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)