Skip to content

Commit 03bc400

Browse files
Merge pull request #436 from SyncfusionExamples/ES-957455-Apply-shading-to-fields
ES-957455- Add the sample Apply-shading-to-fields
2 parents 163a447 + 67d13cb commit 03bc400

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-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 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-highlight-color-to-fields", "Apply-highlight-color-to-fields\Apply-highlight-color-to-fields.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Apply_highlight_color_to_fields</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Output\.gitkeep">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.Drawing;
4+
using System.IO;
5+
6+
namespace Apply_highlight_color_to_fields
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
// Creates a new instance of WordDocument to work with.
13+
using (WordDocument document = new WordDocument())
14+
{
15+
// Add a new section to the Word document.
16+
IWSection section = document.AddSection();
17+
IWParagraph paragraph = section.AddParagraph();
18+
19+
// Add text before the field (e.g., "Date Field - ").
20+
IWTextRange firstText = paragraph.AppendText("Date Field - ");
21+
22+
// Adds a new Date field to the paragraph with the specified format.
23+
WField field = paragraph.AppendField("Date", FieldType.FieldDate) as WField;
24+
// Set the field code to display the date in "MMMM d, yyyy" format.
25+
field.FieldCode = @"DATE \@" + "\"MMMM d, yyyy\"";
26+
27+
// Reference the field as an entity.
28+
IEntity entity = field;
29+
30+
// Apply shading to the field.
31+
ApplyShading(entity);
32+
33+
// Add another paragraph for the "If" field.
34+
paragraph = section.AddParagraph();
35+
paragraph.AppendText("If Field - ");
36+
37+
// Creates a new IF field.
38+
field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;
39+
// Specifies the field code for the IF statement with true and false branches.
40+
field.FieldCode = "IF \"True\" = \"True\" \"The given statement is Correct\" \"The given statement is Wrong\"";
41+
entity = field;
42+
43+
// Apply shading to the field.
44+
ApplyShading(entity);
45+
46+
// Updates the fields in the document.
47+
document.UpdateDocumentFields();
48+
49+
// Create a file stream to save the document.
50+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
51+
{
52+
// Saves the Word document to the specified file stream in DOCX format.
53+
document.Save(outputFileStream, FormatType.Docx);
54+
}
55+
}
56+
}
57+
58+
// Method to apply shading (highlight) to the content of a field.
59+
static void ApplyShading(IEntity entity)
60+
{
61+
// Loop through the siblings of the current entity (field and its contents) until reaching the FieldEnd.
62+
while (entity.NextSibling != null)
63+
{
64+
// Check if the entity is a text range.
65+
if (entity is WTextRange)
66+
{
67+
// Set the highlight color to LightGray for the text range.
68+
(entity as WTextRange).CharacterFormat.HighlightColor = Color.LightGray;
69+
}
70+
// Check if the entity is a field mark and is of FieldEnd type.
71+
else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd)
72+
{
73+
// Break out of the loop once the end of the field is reached.
74+
break;
75+
}
76+
77+
// Move to the next sibling entity (next part of the field).
78+
entity = entity.NextSibling;
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)