-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
90 lines (68 loc) · 2.9 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;
namespace RichEditDOCVARIABLEBasics {
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1() {
InitializeComponent();
List<DetailInfo> details = new List<DetailInfo>
{
new DetailInfo(1, "Documents//Detail1"),
new DetailInfo(2, "Documents//Detail2")
};
richEditControl1.Options.MailMerge.DataSource = details;
richEditControl1.Document.CalculateDocumentVariable += new CalculateDocumentVariableEventHandler(Document_CalculateDocumentVariable);
richEditControl1.LoadDocument("Documents//Template.rtf");
ShowFieldCodes();
}
private void button1_Click(object sender, EventArgs e) {
richEditControl1.LoadDocument("Template.rtf");
ShowFieldCodes();
}
private void button2_Click(object sender, EventArgs e) {
MailMergeOptions myMergeOptions = richEditControl1.Document.CreateMailMergeOptions();
myMergeOptions.MergeMode = MergeMode.NewParagraph;
RichEditDocumentServer server = new RichEditDocumentServer();
server.CalculateDocumentVariable += new CalculateDocumentVariableEventHandler(Document_CalculateDocumentVariable);
richEditControl1.Document.MailMerge(myMergeOptions, server.Document);
richEditControl1.CreateNewDocument();
richEditControl1.Document.AppendDocumentContent(server.Document.Range);
}
void Document_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e)
{
int detailId = -1;
if (Int32.TryParse(e.Arguments[0].Value, out detailId)) {
RichEditDocumentServer server = new RichEditDocumentServer();
string path = string.Format("{0}\\Documents\\Detail{1}.rtf", System.IO.Directory.GetCurrentDirectory(), detailId.ToString());
server.LoadDocument(path);
e.Value = server;
e.Handled = true;
}
}
private void ShowFieldCodes() {
Document doc = richEditControl1.Document;
doc.BeginUpdate();
foreach (Field f in doc.Fields) f.ShowCodes = true;
doc.EndUpdate();
}
}
public class DetailInfo {
private int id;
public int DetailId {
get { return id; }
set { id = value; }
}
private string description;
public string Description {
get { return description; }
set { description = value; }
}
public DetailInfo(int id, string description) {
this.DetailId = id;
this.Description = description;
}
}
}