-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
106 lines (87 loc) · 3.68 KB
/
Form1.cs
File metadata and controls
106 lines (87 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TXTextControl;
using TXTextControl.DocumentServer.Fields;
namespace tx_custom_fields_SUM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void loadTemplateToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadSettings ls = new LoadSettings();
ls.ApplicationFieldFormat = ApplicationFieldFormat.MSWord;
textControl1.Load("template.docx", StreamType.WordprocessingML, ls);
}
private void mergeToolStripMenuItem_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml("sample_db.xml", XmlReadMode.Auto);
mailMerge1.Merge(ds.Tables[0]);
}
private void mailMerge1_FieldMerged(object sender, TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e)
{
// set the field value to keep field functionality
// fields that are set in the event are not removed
e.MergedField = e.MergedField;
}
private void mailMerge1_DataRowMerged(object sender, TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
{
byte[] data = null;
// create a temporary ServerTextControl to work on the block data
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
// load the block data into the temporary ServerTextControl
tx.Load(e.MergedRow, BinaryStreamType.InternalUnicodeFormat);
// loop #1 to find all SUM fields
foreach (ApplicationField sumField in tx.ApplicationFields)
{
if (sumField.TypeName != "MERGEFIELD")
continue;
MergeField mf = new MergeField(sumField);
// if SUM field found, loop through all fields again total them up
if (mf.Name.StartsWith("SUM:") == true)
{
decimal fSum = 0.0M;
// loop #2
foreach (ApplicationField appField in tx.ApplicationFields)
{
if (appField.TypeName != "MERGEFIELD")
continue;
MergeField mfa = new MergeField(appField);
if (mfa.Name == mf.Name.Substring(4))
{
fSum += Decimal.Parse(mfa.Text,
NumberStyles.AllowCurrencySymbol |
NumberStyles.AllowDecimalPoint |
NumberStyles.AllowThousands,
new CultureInfo("en-US"));
}
}
// set the total number
mf.Text = fSum.ToString();
}
}
// save the complete block to a byte[] array
tx.Save(out data, BinaryStreamType.InternalUnicodeFormat);
}
// load back to the byte[] array to the MailMerge process
e.MergedRow = data;
}
private void mailMergeToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
mergeToolStripMenuItem.Enabled = (textControl1.ApplicationFields.Count > 0) ? true : false;
}
}
}