-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
86 lines (69 loc) · 3.46 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
using System;
using System.Diagnostics;
using System.Windows.Forms;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Services;
using DevExpress.Office.Services;
namespace RichEditImageMailMerge {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
RegisterUriStreamService(richEditControl1);
richEditControl1.LoadDocument(Application.StartupPath + @"\..\..\" + "MailMergeTemplate.rtf");
}
private void richEditControl1_DocumentLoaded(object sender, EventArgs e) {
richEditControl1.Options.MailMerge.DataSource = NorthwindDataProvider.Categories;
richEditControl1.Options.MailMerge.ViewMergedData = true;
dataNavigator1.DataSource = NorthwindDataProvider.Categories;
}
#region #registerprovider
private void RegisterUriStreamService(RichEditControl richEditControl) {
IUriStreamService uriStreamService = richEditControl.GetService<IUriStreamService>();
uriStreamService.RegisterProvider(new ImageStreamProvider(NorthwindDataProvider.Categories, "Picture"));
}
#endregion #registerprovider
#region Merge Several Records
private void mergeToNewDocumentItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
MergeToNewDocument();
}
private void MergeToNewDocument() {
MergeSimpleDataForm form = new MergeSimpleDataForm();
if (form.ShowDialog(this) == DialogResult.OK) {
MailMergeOptions options = richEditControl1.Document.CreateMailMergeOptions();
CalculateMailMergeOptions(form, options);
options.MergeMode = form.MergeMode;
if (form.MergeDestination == MergeDestination.NewTab)
MailMergeToNewControl(options);
else
MailMergeToNewFile(options);
}
}
private void CalculateMailMergeOptions(MergeSimpleDataForm form, MailMergeOptions options) {
if (form.MergeRecords == MergeRecords.FromInterval) {
options.FirstRecordIndex = Math.Max(0, ((MergeSimpleDataForm)form).StartIndex - 1);
options.LastRecordIndex = Math.Min(NorthwindDataProvider.Categories.Rows.Count - 1, ((MergeSimpleDataForm)form).EndIndex - 1);
}
else if (form.MergeRecords == MergeRecords.Current) {
options.FirstRecordIndex = dataNavigator1.Position;
options.LastRecordIndex = dataNavigator1.Position;
}
}
private void MailMergeToNewControl(MailMergeOptions options) {
Form form = new Form();
RichEditControl control = new RichEditControl();
richEditControl1.Document.MailMerge(options, control.Document);
control.Dock = DockStyle.Fill;
form.Controls.Add(control);
form.Show(this);
}
private void MailMergeToNewFile(MailMergeOptions options) {
string fileName = System.IO.Directory.GetCurrentDirectory() + @"\..\..\MailMergeResult.rtf";
richEditControl1.Document.MailMerge(options, fileName, DocumentFormat.Rtf);
Process process = new Process();
process.StartInfo.FileName = fileName;
process.Start();
}
#endregion Merge Several Records
}
}