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,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generate-color-coded-invoices", "Generate-color-coded-invoices\Generate-color-coded-invoices.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file not shown.
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>Generate_color_coded_invoices</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<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,61 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.Data;
using Syncfusion.Drawing;


// Holds row index to color mapping
Dictionary<int, object> TextColors = new Dictionary<int, object>();
// Load the Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{

// Get invoice data
DataTable invoiceTable = GetInvoiceData();

// Store color info by row index
for (int i = 0; i < invoiceTable.Rows.Count; i++)
TextColors.Add(i, invoiceTable.Rows[i]["FontColor"]);

// Hook merge event to apply font color
document.MailMerge.MergeField += ApplyColorToFields;

// Enable separate page for each invoice
document.MailMerge.StartAtNewPage = true;

// Perform mail merge
document.MailMerge.ExecuteGroup(invoiceTable);

// Save the modified document
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
}

// Event handler to apply text color based on row
void ApplyColorToFields(object sender, MergeFieldEventArgs args)
{
if (TextColors.TryGetValue(args.RowIndex, out object color))
args.TextRange.CharacterFormat.TextColor = (Color)color;
}

// Generates sample invoice data
DataTable GetInvoiceData()
{
DataTable table = new DataTable("Invoice");

table.Columns.Add("InvoiceNumber");
table.Columns.Add("InvoiceDate");
table.Columns.Add("CustomerName");
table.Columns.Add("ItemDescription");
table.Columns.Add("Amount");
table.Columns.Add("FontColor", typeof(Color));

// First Invoice
table.Rows.Add("INV001", "2024-05-01", "Andy Bernard", "Consulting Services", "$3000.00", Color.Teal);
// Second Invoice
table.Rows.Add("INV002", "2024-05-05", "Stanley Hudson", "Software Development", "$4500.00", Color.DarkOrange);
// Third Invoice
table.Rows.Add("INV003", "2024-05-10", "Margaret Peacock", "UI Design Services", "$2000.00", Color.Indigo);

return table;
}

Loading