Skip to content

Commit

Permalink
Merge pull request #236 from SyncfusionExamples/825469-change-font-fo…
Browse files Browse the repository at this point in the history
…r-all-text

825469-change-font-for-all-text
  • Loading branch information
MohanaselvamJothi authored Oct 4, 2024
2 parents 295c6a7 + efe0f5e commit 715f271
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
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.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17B1609A-9006-4997-82C7-3A2FEEA82657}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {28C9FA54-8845-4B02-86AD-5B7AA7EEC783}
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_font_for_all_text</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.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,54 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

class Program
{
static void Main(string[] args)
{
// Create an input file stream to open the document
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read))
{
// Create a new Word document instance from the input stream
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
{
// Change the font of the document text to "Times New Roman"
ChangeFontName(document, "Times New Roman");

// Create an output file stream to save the modified document
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
// Save the modified document to the output stream in DOCX format
document.Save(outputStream, FormatType.Docx);
}
}
}
}

/// <summary>
/// Changes the font name of all text and paragraph marks in the Word document.
/// </summary>
/// <param name="document">The WordDocument object to modify.</param>
/// <param name="fontName">The new font name to apply.</param>
private static void ChangeFontName(WordDocument document, string fontName)
{
// Find all paragraphs by EntityType in the Word document.
List<Entity> paragraphs = document.FindAllItemsByProperty(EntityType.Paragraph, null, null);

// Change the font name for all paragraph marks (non-printing characters).
for (int i = 0; i < paragraphs.Count; i++)
{
WParagraph paragraph = paragraphs[i] as WParagraph;
paragraph.BreakCharacterFormat.FontName = fontName;
}

// Find all text ranges by EntityType in the Word document.
List<Entity> textRanges = document.FindAllItemsByProperty(EntityType.TextRange, null, null);

// Change the font name for all text content in the document.
for (int i = 0; i < textRanges.Count; i++)
{
WTextRange textRange = textRanges[i] as WTextRange;
textRange.CharacterFormat.FontName = fontName;
}
}
}

0 comments on commit 715f271

Please sign in to comment.