Skip to content

Commit b409b67

Browse files
Added sample
1 parent 122d5fe commit b409b67

6 files changed

Lines changed: 174 additions & 0 deletions

File tree

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.14.37216.2 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Set_Font_Size_for_Text_Highlighted_Rows", "Set_Font_Size_for_Text_Highlighted_Rows\Set_Font_Size_for_Text_Highlighted_Rows.csproj", "{6F4AB1AA-313D-E96E-2EC2-40D7FC31DEE8}"
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+
{6F4AB1AA-313D-E96E-2EC2-40D7FC31DEE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6F4AB1AA-313D-E96E-2EC2-40D7FC31DEE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6F4AB1AA-313D-E96E-2EC2-40D7FC31DEE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6F4AB1AA-313D-E96E-2EC2-40D7FC31DEE8}.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 = {6B9A86AF-F86F-4630-A665-FA532BBAB2A9}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Set_Font_Size_for_Text_Highlighted_Rows
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
11+
{
12+
//Opens an existing document.
13+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
14+
{
15+
foreach (WSection section in document.Sections)
16+
{
17+
//Accesses the Body of section where all the contents in document are apart.
18+
IterateTextBody(section.Body);
19+
}
20+
//Creates file stream.
21+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
22+
{
23+
//Saves the Word document to file stream.
24+
document.Save(outputFileStream, FormatType.Docx);
25+
}
26+
}
27+
}
28+
}
29+
/// <summary>
30+
/// Iterates textbody child elements.
31+
/// </summary>
32+
private static void IterateTextBody(WTextBody textBody)
33+
{
34+
//Iterates through each of the child items of WTextBody.
35+
for (int i = 0; i < textBody.ChildEntities.Count; i++)
36+
{
37+
//IEntity is the basic unit in DocIO DOM.
38+
//Accesses the body items (should be either paragraph, table or block content control) as IEntity.
39+
IEntity bodyItemEntity = textBody.ChildEntities[i];
40+
//A Text body has 3 types of elements - Paragraph, Table and Block Content Control
41+
//Decides the element type by using EntityType.
42+
switch (bodyItemEntity.EntityType)
43+
{
44+
case EntityType.Paragraph:
45+
WParagraph paragraph = bodyItemEntity as WParagraph;
46+
//Processes the paragraph contents.
47+
//Iterates through the paragraph's DOM.
48+
IterateParagraph(paragraph.Items);
49+
break;
50+
case EntityType.Table:
51+
//Table is a collection of rows and cells.
52+
//Iterates through table's DOM.
53+
IterateTable(bodyItemEntity as WTable);
54+
break;
55+
case EntityType.BlockContentControl:
56+
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
57+
//Iterates to the body items of Block Content Control.
58+
IterateTextBody(blockContentControl.TextBody);
59+
break;
60+
}
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Iterates table child elements.
66+
/// </summary>
67+
private static void IterateTable(WTable table)
68+
{
69+
//Iterates the row collection in a table.
70+
foreach (WTableRow row in table.Rows)
71+
{
72+
//Iterates the cell collection in a table row.
73+
foreach (WTableCell cell in row.Cells)
74+
{
75+
//Table cell is derived from (also a) TextBody.
76+
//Reusing the code meant for iterating TextBody.
77+
IterateTextBody(cell);
78+
}
79+
}
80+
}
81+
82+
/// <summary>
83+
/// Iterates paragraph child elements.
84+
/// </summary>
85+
private static void IterateParagraph(ParagraphItemCollection paraItems)
86+
{
87+
for (int i = 0; i < paraItems.Count; i++)
88+
{
89+
Entity entity = paraItems[i];
90+
//A paragraph can have child elements such as text, image, hyperlink, symbols, etc.,
91+
//Decides the element type by using EntityType.
92+
switch (entity.EntityType)
93+
{
94+
case EntityType.TextRange:
95+
//Replaces the text with another.
96+
WTextRange textRange = entity as WTextRange;
97+
// Get character format of the text
98+
WCharacterFormat charFormat = textRange.CharacterFormat;
99+
//Checks whether text has highlightcolor
100+
if (!charFormat.HighlightColor.IsEmpty)
101+
{
102+
//If text has highlight color, set text's font size larger
103+
charFormat.FontSize = 14;
104+
}
105+
break;
106+
case EntityType.TextBox:
107+
//Iterates to the body items of textbox.
108+
WTextBox textBox = entity as WTextBox;
109+
IterateTextBody(textBox.TextBoxBody);
110+
break;
111+
case EntityType.Shape:
112+
//Iterates to the body items of shape.
113+
Shape shape = entity as Shape;
114+
IterateTextBody(shape.TextBody);
115+
break;
116+
case EntityType.InlineContentControl:
117+
//Iterates to the paragraph items of inline content control.
118+
InlineContentControl inlineContentControl = entity as InlineContentControl;
119+
IterateParagraph(inlineContentControl.ParagraphItems);
120+
break;
121+
}
122+
}
123+
}
124+
}
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<None Update="Data\Template.docx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Output\.gitkeep">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)