diff --git a/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields.sln b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields.sln new file mode 100644 index 000000000..eb075d08c --- /dev/null +++ b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31911.196 +MinimumVisualStudioVersion = 10.0.40219.1 +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}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4} + EndGlobalSection +EndGlobal diff --git a/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Apply-highlight-color-to-fields.csproj b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Apply-highlight-color-to-fields.csproj new file mode 100644 index 000000000..641f53f99 --- /dev/null +++ b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Apply-highlight-color-to-fields.csproj @@ -0,0 +1,19 @@ + + + + Exe + net8.0 + Apply_highlight_color_to_fields + + + + + + + + + Always + + + + diff --git a/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Output/.gitkeep b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Program.cs b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Program.cs new file mode 100644 index 000000000..00f22b149 --- /dev/null +++ b/Fields/Apply-highlight-color-to-fields/.NET/Apply-highlight-color-to-fields/Program.cs @@ -0,0 +1,82 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.Drawing; +using System.IO; + +namespace Apply_highlight_color_to_fields +{ + class Program + { + static void Main(string[] args) + { + // Creates a new instance of WordDocument to work with. + using (WordDocument document = new WordDocument()) + { + // Add a new section to the Word document. + IWSection section = document.AddSection(); + IWParagraph paragraph = section.AddParagraph(); + + // Add text before the field (e.g., "Date Field - "). + IWTextRange firstText = paragraph.AppendText("Date Field - "); + + // Adds a new Date field to the paragraph with the specified format. + WField field = paragraph.AppendField("Date", FieldType.FieldDate) as WField; + // Set the field code to display the date in "MMMM d, yyyy" format. + field.FieldCode = @"DATE \@" + "\"MMMM d, yyyy\""; + + // Reference the field as an entity. + IEntity entity = field; + + // Apply shading to the field. + ApplyShading(entity); + + // Add another paragraph for the "If" field. + paragraph = section.AddParagraph(); + paragraph.AppendText("If Field - "); + + // Creates a new IF field. + field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField; + // Specifies the field code for the IF statement with true and false branches. + field.FieldCode = "IF \"True\" = \"True\" \"The given statement is Correct\" \"The given statement is Wrong\""; + entity = field; + + // Apply shading to the field. + ApplyShading(entity); + + // Updates the fields in the document. + document.UpdateDocumentFields(); + + // Create a file stream to save the document. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) + { + // Saves the Word document to the specified file stream in DOCX format. + document.Save(outputFileStream, FormatType.Docx); + } + } + } + + // Method to apply shading (highlight) to the content of a field. + static void ApplyShading(IEntity entity) + { + // Loop through the siblings of the current entity (field and its contents) until reaching the FieldEnd. + while (entity.NextSibling != null) + { + // Check if the entity is a text range. + if (entity is WTextRange) + { + // Set the highlight color to LightGray for the text range. + (entity as WTextRange).CharacterFormat.HighlightColor = Color.LightGray; + } + // Check if the entity is a field mark and is of FieldEnd type. + else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd) + { + // Break out of the loop once the end of the field is reached. + break; + } + + // Move to the next sibling entity (next part of the field). + entity = entity.NextSibling; + } + } + } +}