Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Apply_highlight_color_to_fields</RootNamespace>
</PropertyGroup>

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

<ItemGroup>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
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,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;
}
}
}
}
Loading