Skip to content

Commit 7a67cab

Browse files
Merge pull request #441 from SyncfusionExamples/ES-944860-Generate-color-coded-invoices
ES-944860- Add the sample Generate-color-coded-invoices
2 parents 0441582 + f913224 commit 7a67cab

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generate-color-coded-invoices", "Generate-color-coded-invoices\Generate-color-coded-invoices.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
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+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Generate_color_coded_invoices</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Data;
4+
using Syncfusion.Drawing;
5+
6+
7+
// Holds row index to color mapping
8+
Dictionary<int, object> TextColors = new Dictionary<int, object>();
9+
// Load the Word document
10+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
11+
{
12+
13+
// Get invoice data
14+
DataTable invoiceTable = GetInvoiceData();
15+
16+
// Store color info by row index
17+
for (int i = 0; i < invoiceTable.Rows.Count; i++)
18+
TextColors.Add(i, invoiceTable.Rows[i]["FontColor"]);
19+
20+
// Hook merge event to apply font color
21+
document.MailMerge.MergeField += ApplyColorToFields;
22+
23+
// Enable separate page for each invoice
24+
document.MailMerge.StartAtNewPage = true;
25+
26+
// Perform mail merge
27+
document.MailMerge.ExecuteGroup(invoiceTable);
28+
29+
// Save the modified document
30+
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
31+
}
32+
33+
// Event handler to apply text color based on row
34+
void ApplyColorToFields(object sender, MergeFieldEventArgs args)
35+
{
36+
if (TextColors.TryGetValue(args.RowIndex, out object color))
37+
args.TextRange.CharacterFormat.TextColor = (Color)color;
38+
}
39+
40+
// Generates sample invoice data
41+
DataTable GetInvoiceData()
42+
{
43+
DataTable table = new DataTable("Invoice");
44+
45+
table.Columns.Add("InvoiceNumber");
46+
table.Columns.Add("InvoiceDate");
47+
table.Columns.Add("CustomerName");
48+
table.Columns.Add("ItemDescription");
49+
table.Columns.Add("Amount");
50+
table.Columns.Add("FontColor", typeof(Color));
51+
52+
// First Invoice
53+
table.Rows.Add("INV001", "2024-05-01", "Andy Bernard", "Consulting Services", "$3000.00", Color.Teal);
54+
// Second Invoice
55+
table.Rows.Add("INV002", "2024-05-05", "Stanley Hudson", "Software Development", "$4500.00", Color.DarkOrange);
56+
// Third Invoice
57+
table.Rows.Add("INV003", "2024-05-10", "Margaret Peacock", "UI Design Services", "$2000.00", Color.Indigo);
58+
59+
return table;
60+
}
61+

0 commit comments

Comments
 (0)