-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from SyncfusionExamples/825469-change-font-fo…
…r-all-text 825469-change-font-for-all-text
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Paragraphs/Change-font-for-all-text/.NET/Change-font-for-all-text.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
24 changes: 24 additions & 0 deletions
24
...hs/Change-font-for-all-text/.NET/Change-font-for-all-text/Change-font-for-all-text.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+155 KB
Paragraphs/Change-font-for-all-text/.NET/Change-font-for-all-text/Data/Input.docx
Binary file not shown.
1 change: 1 addition & 0 deletions
1
Paragraphs/Change-font-for-all-text/.NET/Change-font-for-all-text/Output/.gitkeep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
54 changes: 54 additions & 0 deletions
54
Paragraphs/Change-font-for-all-text/.NET/Change-font-for-all-text/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |