-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (94 loc) · 4.55 KB
/
Program.cs
File metadata and controls
115 lines (94 loc) · 4.55 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
107
108
109
110
111
112
113
114
115
using DevExpress.Office.Services;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Data;
using System.Diagnostics;
namespace WordProcessingMailMerge
{
internal class Program
{
static void Main(string[] args)
{
DataSet xmlDataSet = new DataSet();
xmlDataSet.ReadXml("..\\..\\..\\Employees.xml");
xmlDataSet.Tables[0].PrimaryKey = new DataColumn[] { xmlDataSet.Tables[0].Columns[0] };
using (var wordProcessor = new RichEditDocumentServer())
{
wordProcessor.LoadDocument("..\\..\\..\\template.docx");
AddMailMergeFields(wordProcessor.Document);
wordProcessor.CalculateDocumentVariable += WordProcessor_CalculateDocumentVariable;
// Register the URI provider service
IUriStreamService uriStreamService = wordProcessor.GetService<IUriStreamService>();
uriStreamService.RegisterProvider(new ImageStreamProvider(xmlDataSet.Tables[0], "Photo"));
MailMergeOptions myMergeOptions =
wordProcessor.Document.CreateMailMergeOptions();
myMergeOptions.DataSource = xmlDataSet.Tables[0];
myMergeOptions.MergeMode = MergeMode.NewSection;
wordProcessor.MailMerge(myMergeOptions, "result.docx", DocumentFormat.OpenXml);
}
Process.Start(new ProcessStartInfo("result.docx") { UseShellExecute = true });
}
private static void AddMailMergeFields(Document document)
{
DocumentRange[] pictureRanges = document.FindAll("Photo", SearchOptions.WholeWord);
foreach (var pictureRange in pictureRanges)
{
DocumentPosition picturePosition = pictureRange.End;
// Delete the placeholder
document.Delete(pictureRange);
// Insert a field to a picture from a database
Field pictureField = document.Fields.Create(picturePosition, @"INCLUDEPICTURE ""dbimg://{ placeholder }""");
// Find a placeholder for a nested MERGEFIELD
DocumentRange nestedFieldRange =
document.FindAll("{ placeholder }", SearchOptions.WholeWord, pictureField.CodeRange).First();
// Clear the placeholder range
document.Delete(nestedFieldRange);
// Create a nested field
document.Fields.Create(nestedFieldRange.Start, "MERGEFIELD EmployeeID");
}
// Find a placeholder for another image
// in the footer
SubDocument footer = document.Sections[0].BeginUpdateFooter();
DocumentRange[] imageRanges =
footer.FindAll("image", SearchOptions.WholeWord);
foreach (var imageRange in imageRanges)
{
DocumentPosition imagePosition = imageRange.End;
// Delete the phrase
footer.Delete(imageRange);
// Create a field at the placeholder's position
footer.Fields.Create(imagePosition, @"INCLUDEPICTURE ""DevExpress.png""");
}
footer.EndUpdate();
// Find a placeholder for the FirstName field:
DocumentRange[] nameRanges =
document.FindAll("FirstName", SearchOptions.WholeWord);
foreach (var nameRange in nameRanges)
{
DocumentPosition namePosition = nameRange.End;
// Delete the phrase
document.Delete(nameRange);
// Create a field at the placeholder position
document.Fields.Create(namePosition, @"MERGEFIELD ""FirstName""");
}
}
private static void WordProcessor_CalculateDocumentVariable(object sender, CalculateDocumentVariableEventArgs e)
{
if (e.Arguments.Count > 0)
{
// Retrieve the MERGEFIELD field value
DateTimeOffset hireDate = Convert.ToDateTime(e.Arguments[0].Value);
DateTimeOffset currentDate = DateTime.Now;
// Calculate the difference between the current date
// and the hire date
var dif = currentDate.Subtract(hireDate);
// Calculate the number of years
int years = dif.Days / 365;
// Specify the DOCVARIABLE field value
e.Value = years.ToString();
e.Handled = true;
}
e.Handled = true;
}
}
}