Skip to content

Commit 715f271

Browse files
Merge pull request #236 from SyncfusionExamples/825469-change-font-for-all-text
825469-change-font-for-all-text
2 parents 295c6a7 + efe0f5e commit 715f271

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-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-font-for-all-text", "Change-font-for-all-text\Change-font-for-all-text.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_font_for_all_text</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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
class Program
5+
{
6+
static void Main(string[] args)
7+
{
8+
// Create an input file stream to open the document
9+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
10+
{
11+
// Create a new Word document instance from the input stream
12+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
13+
{
14+
// Change the font of the document text to "Times New Roman"
15+
ChangeFontName(document, "Times New Roman");
16+
17+
// Create an output file stream to save the modified document
18+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
19+
{
20+
// Save the modified document to the output stream in DOCX format
21+
document.Save(outputStream, FormatType.Docx);
22+
}
23+
}
24+
}
25+
}
26+
27+
/// <summary>
28+
/// Changes the font name of all text and paragraph marks in the Word document.
29+
/// </summary>
30+
/// <param name="document">The WordDocument object to modify.</param>
31+
/// <param name="fontName">The new font name to apply.</param>
32+
private static void ChangeFontName(WordDocument document, string fontName)
33+
{
34+
// Find all paragraphs by EntityType in the Word document.
35+
List<Entity> paragraphs = document.FindAllItemsByProperty(EntityType.Paragraph, null, null);
36+
37+
// Change the font name for all paragraph marks (non-printing characters).
38+
for (int i = 0; i < paragraphs.Count; i++)
39+
{
40+
WParagraph paragraph = paragraphs[i] as WParagraph;
41+
paragraph.BreakCharacterFormat.FontName = fontName;
42+
}
43+
44+
// Find all text ranges by EntityType in the Word document.
45+
List<Entity> textRanges = document.FindAllItemsByProperty(EntityType.TextRange, null, null);
46+
47+
// Change the font name for all text content in the document.
48+
for (int i = 0; i < textRanges.Count; i++)
49+
{
50+
WTextRange textRange = textRanges[i] as WTextRange;
51+
textRange.CharacterFormat.FontName = fontName;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)